Revert "Rename Packets to PacketBatch (backport #21794) (#21804)"

This reverts commit 39e27b130f.
This commit is contained in:
Trent Nelson
2021-12-13 08:48:37 -07:00
committed by Tao Zhu
parent 09c68ce696
commit 13d40d6a66
25 changed files with 645 additions and 695 deletions

View File

@@ -12,10 +12,10 @@ use {
solana_metrics::inc_new_counter_debug,
solana_perf::{
cuda_runtime::PinnedVec,
packet::{limited_deserialize, Packet, PacketBatch},
packet::{limited_deserialize, Packet, Packets},
perf_libs,
recycler_cache::RecyclerCache,
sigverify::{self, count_packets_in_batches, TxOffset},
sigverify::{self, batch_size, TxOffset},
},
solana_rayon_threadlimit::get_thread_count,
solana_sdk::{
@@ -76,26 +76,22 @@ pub fn verify_shred_cpu(packet: &Packet, slot_leaders: &HashMap<u64, [u8; 32]>)
Some(1)
}
fn verify_shreds_cpu(
batches: &[PacketBatch],
slot_leaders: &HashMap<u64, [u8; 32]>,
) -> Vec<Vec<u8>> {
fn verify_shreds_cpu(batches: &[Packets], slot_leaders: &HashMap<u64, [u8; 32]>) -> Vec<Vec<u8>> {
use rayon::prelude::*;
let packet_count = count_packets_in_batches(batches);
debug!("CPU SHRED ECDSA for {}", packet_count);
let count = batch_size(batches);
debug!("CPU SHRED ECDSA for {}", count);
let rv = SIGVERIFY_THREAD_POOL.install(|| {
batches
.into_par_iter()
.map(|batch| {
batch
.packets
.map(|p| {
p.packets
.par_iter()
.map(|p| verify_shred_cpu(p, slot_leaders).unwrap_or(0))
.collect()
})
.collect()
});
inc_new_counter_debug!("ed25519_shred_verify_cpu", packet_count);
inc_new_counter_debug!("ed25519_shred_verify_cpu", count);
rv
}
@@ -103,7 +99,7 @@ fn slot_key_data_for_gpu<
T: Sync + Sized + Default + std::fmt::Debug + Eq + std::hash::Hash + Clone + Copy + AsRef<[u8]>,
>(
offset_start: usize,
batches: &[PacketBatch],
batches: &[Packets],
slot_keys: &HashMap<u64, T>,
recycler_cache: &RecyclerCache,
) -> (PinnedVec<u8>, TxOffset, usize) {
@@ -112,9 +108,8 @@ fn slot_key_data_for_gpu<
let slots: Vec<Vec<Slot>> = SIGVERIFY_THREAD_POOL.install(|| {
batches
.into_par_iter()
.map(|batch| {
batch
.packets
.map(|p| {
p.packets
.iter()
.map(|packet| {
let slot_start = size_of::<Signature>() + size_of::<ShredType>();
@@ -178,7 +173,7 @@ fn vec_size_in_packets(keyvec: &PinnedVec<u8>) -> usize {
}
fn resize_vec(keyvec: &mut PinnedVec<u8>) -> usize {
//HACK: Pubkeys vector is passed along as a `PacketBatch` buffer to the GPU
//HACK: Pubkeys vector is passed along as a `Packets` buffer to the GPU
//TODO: GPU needs a more opaque interface, which can handle variable sized structures for data
//Pad the Pubkeys buffer such that it is bigger than a buffer of Packet sized elems
let num_in_packets = (keyvec.len() + (size_of::<Packet>() - 1)) / size_of::<Packet>();
@@ -188,7 +183,7 @@ fn resize_vec(keyvec: &mut PinnedVec<u8>) -> usize {
fn shred_gpu_offsets(
mut pubkeys_end: usize,
batches: &[PacketBatch],
batches: &[Packets],
recycler_cache: &RecyclerCache,
) -> (TxOffset, TxOffset, TxOffset, Vec<Vec<u32>>) {
let mut signature_offsets = recycler_cache.offsets().allocate("shred_signatures");
@@ -226,7 +221,7 @@ fn shred_gpu_offsets(
}
pub fn verify_shreds_gpu(
batches: &[PacketBatch],
batches: &[Packets],
slot_leaders: &HashMap<u64, [u8; 32]>,
recycler_cache: &RecyclerCache,
) -> Vec<Vec<u8>> {
@@ -238,10 +233,10 @@ pub fn verify_shreds_gpu(
let mut elems = Vec::new();
let mut rvs = Vec::new();
let packet_count = count_packets_in_batches(batches);
let count = batch_size(batches);
let (pubkeys, pubkey_offsets, mut num_packets) =
slot_key_data_for_gpu(0, batches, slot_leaders, recycler_cache);
//HACK: Pubkeys vector is passed along as a `PacketBatch` buffer to the GPU
//HACK: Pubkeys vector is passed along as a `Packets` buffer to the GPU
//TODO: GPU needs a more opaque interface, which can handle variable sized structures for data
let pubkeys_len = num_packets * size_of::<Packet>();
trace!("num_packets: {}", num_packets);
@@ -256,15 +251,15 @@ pub fn verify_shreds_gpu(
num: num_packets as u32,
});
for batch in batches {
for p in batches {
elems.push(perf_libs::Elems {
elems: batch.packets.as_ptr(),
num: batch.packets.len() as u32,
elems: p.packets.as_ptr(),
num: p.packets.len() as u32,
});
let mut v = Vec::new();
v.resize(batch.packets.len(), 0);
v.resize(p.packets.len(), 0);
rvs.push(v);
num_packets += batch.packets.len();
num_packets += p.packets.len();
}
out.resize(signature_offsets.len(), 0);
@@ -295,7 +290,7 @@ pub fn verify_shreds_gpu(
sigverify::copy_return_values(&v_sig_lens, &out, &mut rvs);
inc_new_counter_debug!("ed25519_shred_verify_gpu", packet_count);
inc_new_counter_debug!("ed25519_shred_verify_gpu", count);
rvs
}
@@ -321,18 +316,18 @@ fn sign_shred_cpu(keypair: &Keypair, packet: &mut Packet) {
packet.data[0..sig_end].copy_from_slice(signature.as_ref());
}
pub fn sign_shreds_cpu(keypair: &Keypair, batches: &mut [PacketBatch]) {
pub fn sign_shreds_cpu(keypair: &Keypair, batches: &mut [Packets]) {
use rayon::prelude::*;
let packet_count = count_packets_in_batches(batches);
debug!("CPU SHRED ECDSA for {}", packet_count);
let count = batch_size(batches);
debug!("CPU SHRED ECDSA for {}", count);
SIGVERIFY_THREAD_POOL.install(|| {
batches.par_iter_mut().for_each(|batch| {
batch.packets[..]
batches.par_iter_mut().for_each(|p| {
p.packets[..]
.par_iter_mut()
.for_each(|mut p| sign_shred_cpu(keypair, &mut p));
});
});
inc_new_counter_debug!("ed25519_shred_verify_cpu", packet_count);
inc_new_counter_debug!("ed25519_shred_verify_cpu", count);
}
pub fn sign_shreds_gpu_pinned_keypair(keypair: &Keypair, cache: &RecyclerCache) -> PinnedVec<u8> {
@@ -355,14 +350,14 @@ pub fn sign_shreds_gpu_pinned_keypair(keypair: &Keypair, cache: &RecyclerCache)
pub fn sign_shreds_gpu(
keypair: &Keypair,
pinned_keypair: &Option<Arc<PinnedVec<u8>>>,
batches: &mut [PacketBatch],
batches: &mut [Packets],
recycler_cache: &RecyclerCache,
) {
let sig_size = size_of::<Signature>();
let pubkey_size = size_of::<Pubkey>();
let api = perf_libs::api();
let packet_count = count_packets_in_batches(batches);
if api.is_none() || packet_count < SIGN_SHRED_GPU_MIN || pinned_keypair.is_none() {
let count = batch_size(batches);
if api.is_none() || count < SIGN_SHRED_GPU_MIN || pinned_keypair.is_none() {
return sign_shreds_cpu(keypair, batches);
}
let api = api.unwrap();
@@ -375,10 +370,10 @@ pub fn sign_shreds_gpu(
//should be zero
let mut pubkey_offsets = recycler_cache.offsets().allocate("pubkey offsets");
pubkey_offsets.resize(packet_count, 0);
pubkey_offsets.resize(count, 0);
let mut secret_offsets = recycler_cache.offsets().allocate("secret_offsets");
secret_offsets.resize(packet_count, pubkey_size as u32);
secret_offsets.resize(count, pubkey_size as u32);
trace!("offset: {}", offset);
let (signature_offsets, msg_start_offsets, msg_sizes, _v_sig_lens) =
@@ -393,14 +388,14 @@ pub fn sign_shreds_gpu(
num: num_keypair_packets as u32,
});
for batch in batches.iter() {
for p in batches.iter() {
elems.push(perf_libs::Elems {
elems: batch.packets.as_ptr(),
num: batch.packets.len() as u32,
elems: p.packets.as_ptr(),
num: p.packets.len() as u32,
});
let mut v = Vec::new();
v.resize(batch.packets.len(), 0);
num_packets += batch.packets.len();
v.resize(p.packets.len(), 0);
num_packets += p.packets.len();
}
trace!("Starting verify num packets: {}", num_packets);
@@ -452,7 +447,7 @@ pub fn sign_shreds_gpu(
});
});
});
inc_new_counter_debug!("ed25519_shred_sign_gpu", packet_count);
inc_new_counter_debug!("ed25519_shred_sign_gpu", count);
}
#[cfg(test)]
@@ -511,7 +506,7 @@ pub mod tests {
fn run_test_sigverify_shreds_cpu(slot: Slot) {
solana_logger::setup();
let mut batches = [PacketBatch::default()];
let mut batch = [Packets::default()];
let mut shred = Shred::new_from_data(
slot,
0xc0de,
@@ -525,15 +520,15 @@ pub mod tests {
);
let keypair = Keypair::new();
Shredder::sign_shred(&keypair, &mut shred);
batches[0].packets.resize(1, Packet::default());
batches[0].packets[0].data[0..shred.payload.len()].copy_from_slice(&shred.payload);
batches[0].packets[0].meta.size = shred.payload.len();
batch[0].packets.resize(1, Packet::default());
batch[0].packets[0].data[0..shred.payload.len()].copy_from_slice(&shred.payload);
batch[0].packets[0].meta.size = shred.payload.len();
let leader_slots = [(slot, keypair.pubkey().to_bytes())]
.iter()
.cloned()
.collect();
let rv = verify_shreds_cpu(&batches, &leader_slots);
let rv = verify_shreds_cpu(&batch, &leader_slots);
assert_eq!(rv, vec![vec![1]]);
let wrong_keypair = Keypair::new();
@@ -541,19 +536,19 @@ pub mod tests {
.iter()
.cloned()
.collect();
let rv = verify_shreds_cpu(&batches, &leader_slots);
let rv = verify_shreds_cpu(&batch, &leader_slots);
assert_eq!(rv, vec![vec![0]]);
let leader_slots = HashMap::new();
let rv = verify_shreds_cpu(&batches, &leader_slots);
let rv = verify_shreds_cpu(&batch, &leader_slots);
assert_eq!(rv, vec![vec![0]]);
let leader_slots = [(slot, keypair.pubkey().to_bytes())]
.iter()
.cloned()
.collect();
batches[0].packets[0].meta.size = 0;
let rv = verify_shreds_cpu(&batches, &leader_slots);
batch[0].packets[0].meta.size = 0;
let rv = verify_shreds_cpu(&batch, &leader_slots);
assert_eq!(rv, vec![vec![0]]);
}
@@ -566,7 +561,7 @@ pub mod tests {
solana_logger::setup();
let recycler_cache = RecyclerCache::default();
let mut batches = [PacketBatch::default()];
let mut batch = [Packets::default()];
let mut shred = Shred::new_from_data(
slot,
0xc0de,
@@ -580,9 +575,9 @@ pub mod tests {
);
let keypair = Keypair::new();
Shredder::sign_shred(&keypair, &mut shred);
batches[0].packets.resize(1, Packet::default());
batches[0].packets[0].data[0..shred.payload.len()].copy_from_slice(&shred.payload);
batches[0].packets[0].meta.size = shred.payload.len();
batch[0].packets.resize(1, Packet::default());
batch[0].packets[0].data[0..shred.payload.len()].copy_from_slice(&shred.payload);
batch[0].packets[0].meta.size = shred.payload.len();
let leader_slots = [
(std::u64::MAX, Pubkey::default().to_bytes()),
@@ -591,7 +586,7 @@ pub mod tests {
.iter()
.cloned()
.collect();
let rv = verify_shreds_gpu(&batches, &leader_slots, &recycler_cache);
let rv = verify_shreds_gpu(&batch, &leader_slots, &recycler_cache);
assert_eq!(rv, vec![vec![1]]);
let wrong_keypair = Keypair::new();
@@ -602,14 +597,14 @@ pub mod tests {
.iter()
.cloned()
.collect();
let rv = verify_shreds_gpu(&batches, &leader_slots, &recycler_cache);
let rv = verify_shreds_gpu(&batch, &leader_slots, &recycler_cache);
assert_eq!(rv, vec![vec![0]]);
let leader_slots = [(std::u64::MAX, [0u8; 32])].iter().cloned().collect();
let rv = verify_shreds_gpu(&batches, &leader_slots, &recycler_cache);
let rv = verify_shreds_gpu(&batch, &leader_slots, &recycler_cache);
assert_eq!(rv, vec![vec![0]]);
batches[0].packets[0].meta.size = 0;
batch[0].packets[0].meta.size = 0;
let leader_slots = [
(std::u64::MAX, Pubkey::default().to_bytes()),
(slot, keypair.pubkey().to_bytes()),
@@ -617,7 +612,7 @@ pub mod tests {
.iter()
.cloned()
.collect();
let rv = verify_shreds_gpu(&batches, &leader_slots, &recycler_cache);
let rv = verify_shreds_gpu(&batch, &leader_slots, &recycler_cache);
assert_eq!(rv, vec![vec![0]]);
}
@@ -630,11 +625,11 @@ pub mod tests {
solana_logger::setup();
let recycler_cache = RecyclerCache::default();
let mut packet_batch = PacketBatch::default();
let mut packets = Packets::default();
let num_packets = 32;
let num_batches = 100;
packet_batch.packets.resize(num_packets, Packet::default());
for (i, p) in packet_batch.packets.iter_mut().enumerate() {
packets.packets.resize(num_packets, Packet::default());
for (i, p) in packets.packets.iter_mut().enumerate() {
let shred = Shred::new_from_data(
slot,
0xc0de,
@@ -648,7 +643,7 @@ pub mod tests {
);
shred.copy_to_packet(p);
}
let mut batches = vec![packet_batch; num_batches];
let mut batch = vec![packets; num_batches];
let keypair = Keypair::new();
let pinned_keypair = sign_shreds_gpu_pinned_keypair(&keypair, &recycler_cache);
let pinned_keypair = Some(Arc::new(pinned_keypair));
@@ -660,14 +655,14 @@ pub mod tests {
.cloned()
.collect();
//unsigned
let rv = verify_shreds_gpu(&batches, &pubkeys, &recycler_cache);
let rv = verify_shreds_gpu(&batch, &pubkeys, &recycler_cache);
assert_eq!(rv, vec![vec![0; num_packets]; num_batches]);
//signed
sign_shreds_gpu(&keypair, &pinned_keypair, &mut batches, &recycler_cache);
let rv = verify_shreds_cpu(&batches, &pubkeys);
sign_shreds_gpu(&keypair, &pinned_keypair, &mut batch, &recycler_cache);
let rv = verify_shreds_cpu(&batch, &pubkeys);
assert_eq!(rv, vec![vec![1; num_packets]; num_batches]);
let rv = verify_shreds_gpu(&batches, &pubkeys, &recycler_cache);
let rv = verify_shreds_gpu(&batch, &pubkeys, &recycler_cache);
assert_eq!(rv, vec![vec![1; num_packets]; num_batches]);
}
@@ -679,7 +674,7 @@ pub mod tests {
fn run_test_sigverify_shreds_sign_cpu(slot: Slot) {
solana_logger::setup();
let mut batches = [PacketBatch::default()];
let mut batch = [Packets::default()];
let keypair = Keypair::new();
let shred = Shred::new_from_data(
slot,
@@ -692,9 +687,9 @@ pub mod tests {
0,
0xc0de,
);
batches[0].packets.resize(1, Packet::default());
batches[0].packets[0].data[0..shred.payload.len()].copy_from_slice(&shred.payload);
batches[0].packets[0].meta.size = shred.payload.len();
batch[0].packets.resize(1, Packet::default());
batch[0].packets[0].data[0..shred.payload.len()].copy_from_slice(&shred.payload);
batch[0].packets[0].meta.size = shred.payload.len();
let pubkeys = [
(slot, keypair.pubkey().to_bytes()),
(std::u64::MAX, Pubkey::default().to_bytes()),
@@ -703,11 +698,11 @@ pub mod tests {
.cloned()
.collect();
//unsigned
let rv = verify_shreds_cpu(&batches, &pubkeys);
let rv = verify_shreds_cpu(&batch, &pubkeys);
assert_eq!(rv, vec![vec![0]]);
//signed
sign_shreds_cpu(&keypair, &mut batches);
let rv = verify_shreds_cpu(&batches, &pubkeys);
sign_shreds_cpu(&keypair, &mut batch);
let rv = verify_shreds_cpu(&batch, &pubkeys);
assert_eq!(rv, vec![vec![1]]);
}