2019-10-06 12:56:17 -07:00
|
|
|
#![feature(test)]
|
|
|
|
|
|
|
|
extern crate solana_core;
|
|
|
|
extern crate test;
|
|
|
|
|
|
|
|
use log::*;
|
2019-10-10 13:24:03 -07:00
|
|
|
use solana_core::retransmit_stage::retransmitter;
|
2021-07-14 05:16:29 -07:00
|
|
|
use solana_entry::entry::Entry;
|
2021-05-26 08:15:46 -07:00
|
|
|
use solana_gossip::cluster_info::{ClusterInfo, Node};
|
|
|
|
use solana_gossip::contact_info::ContactInfo;
|
2020-03-21 10:54:40 -07:00
|
|
|
use solana_ledger::genesis_utils::{create_genesis_config, GenesisConfigInfo};
|
2019-10-18 09:28:51 -07:00
|
|
|
use solana_ledger::leader_schedule_cache::LeaderScheduleCache;
|
2020-12-10 07:54:15 -08:00
|
|
|
use solana_ledger::shred::Shredder;
|
2019-10-10 13:24:03 -07:00
|
|
|
use solana_measure::measure::Measure;
|
2020-12-10 07:54:15 -08:00
|
|
|
use solana_perf::packet::{Packet, Packets};
|
2021-05-18 23:54:28 -07:00
|
|
|
use solana_rpc::max_slots::MaxSlots;
|
2019-10-06 12:56:17 -07:00
|
|
|
use solana_runtime::bank::Bank;
|
2020-06-17 08:27:03 -07:00
|
|
|
use solana_runtime::bank_forks::BankForks;
|
2020-12-10 07:54:15 -08:00
|
|
|
use solana_sdk::hash::Hash;
|
2020-10-19 12:23:14 -07:00
|
|
|
use solana_sdk::pubkey;
|
2020-12-10 07:54:15 -08:00
|
|
|
use solana_sdk::signature::{Keypair, Signer};
|
|
|
|
use solana_sdk::system_transaction;
|
2019-10-06 12:56:17 -07:00
|
|
|
use solana_sdk::timing::timestamp;
|
|
|
|
use std::net::UdpSocket;
|
2019-10-10 13:24:03 -07:00
|
|
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
2019-10-06 12:56:17 -07:00
|
|
|
use std::sync::mpsc::channel;
|
2019-10-10 13:24:03 -07:00
|
|
|
use std::sync::Mutex;
|
2019-10-06 12:56:17 -07:00
|
|
|
use std::sync::{Arc, RwLock};
|
2019-10-10 13:24:03 -07:00
|
|
|
use std::thread::sleep;
|
|
|
|
use std::thread::Builder;
|
|
|
|
use std::time::Duration;
|
2019-10-06 12:56:17 -07:00
|
|
|
use test::Bencher;
|
|
|
|
|
|
|
|
#[bench]
|
2020-08-14 11:43:14 -07:00
|
|
|
#[allow(clippy::same_item_push)]
|
2019-10-10 13:24:03 -07:00
|
|
|
fn bench_retransmitter(bencher: &mut Bencher) {
|
2019-10-06 12:56:17 -07:00
|
|
|
solana_logger::setup();
|
2020-04-24 12:03:46 -07:00
|
|
|
let cluster_info = ClusterInfo::new_with_invalid_keypair(Node::new_localhost().info);
|
2019-10-10 13:24:03 -07:00
|
|
|
const NUM_PEERS: usize = 4;
|
|
|
|
let mut peer_sockets = Vec::new();
|
2019-10-06 12:56:17 -07:00
|
|
|
for _ in 0..NUM_PEERS {
|
2021-06-15 06:19:41 -07:00
|
|
|
// This ensures that cluster_info.id() is the root of turbine
|
|
|
|
// retransmit tree and so the shreds are retransmited to all other
|
|
|
|
// nodes in the cluster.
|
|
|
|
let id = std::iter::repeat_with(pubkey::new_rand)
|
|
|
|
.find(|pk| cluster_info.id() < *pk)
|
|
|
|
.unwrap();
|
2019-10-10 13:24:03 -07:00
|
|
|
let socket = UdpSocket::bind("0.0.0.0:0").unwrap();
|
|
|
|
let mut contact_info = ContactInfo::new_localhost(&id, timestamp());
|
|
|
|
contact_info.tvu = socket.local_addr().unwrap();
|
|
|
|
contact_info.tvu.set_ip("127.0.0.1".parse().unwrap());
|
|
|
|
contact_info.tvu_forwards = contact_info.tvu;
|
|
|
|
info!("local: {:?}", contact_info.tvu);
|
2019-10-06 12:56:17 -07:00
|
|
|
cluster_info.insert_info(contact_info);
|
2019-10-10 13:24:03 -07:00
|
|
|
socket.set_nonblocking(true).unwrap();
|
|
|
|
peer_sockets.push(socket);
|
2019-10-06 12:56:17 -07:00
|
|
|
}
|
2019-10-10 13:24:03 -07:00
|
|
|
let peer_sockets = Arc::new(peer_sockets);
|
2020-04-21 12:54:45 -07:00
|
|
|
let cluster_info = Arc::new(cluster_info);
|
2019-10-06 12:56:17 -07:00
|
|
|
|
2019-11-08 20:56:57 -08:00
|
|
|
let GenesisConfigInfo { genesis_config, .. } = create_genesis_config(100_000);
|
|
|
|
let bank0 = Bank::new(&genesis_config);
|
2020-06-12 10:04:17 -07:00
|
|
|
let bank_forks = BankForks::new(bank0);
|
2019-10-06 12:56:17 -07:00
|
|
|
let bank = bank_forks.working_bank();
|
|
|
|
let bank_forks = Arc::new(RwLock::new(bank_forks));
|
|
|
|
let (packet_sender, packet_receiver) = channel();
|
2019-10-10 13:24:03 -07:00
|
|
|
let packet_receiver = Arc::new(Mutex::new(packet_receiver));
|
|
|
|
const NUM_THREADS: usize = 2;
|
|
|
|
let sockets = (0..NUM_THREADS)
|
|
|
|
.map(|_| UdpSocket::bind("0.0.0.0:0").unwrap())
|
|
|
|
.collect();
|
2019-10-06 12:56:17 -07:00
|
|
|
|
|
|
|
let leader_schedule_cache = Arc::new(LeaderScheduleCache::new_from_bank(&bank));
|
|
|
|
|
2019-10-10 13:24:03 -07:00
|
|
|
// To work reliably with higher values, this needs larger udp rmem size
|
2020-12-10 07:54:15 -08:00
|
|
|
let entries: Vec<_> = (0..5)
|
|
|
|
.map(|_| {
|
|
|
|
let keypair0 = Keypair::new();
|
|
|
|
let keypair1 = Keypair::new();
|
|
|
|
let tx0 =
|
|
|
|
system_transaction::transfer(&keypair0, &keypair1.pubkey(), 1, Hash::default());
|
|
|
|
Entry::new(&Hash::default(), 1, vec![tx0])
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
2021-06-21 13:12:38 -07:00
|
|
|
let keypair = Keypair::new();
|
2020-12-10 07:54:15 -08:00
|
|
|
let slot = 0;
|
|
|
|
let parent = 0;
|
2021-06-21 13:12:38 -07:00
|
|
|
let shredder = Shredder::new(slot, parent, 0, 0).unwrap();
|
|
|
|
let mut data_shreds = shredder.entries_to_shreds(&keypair, &entries, true, 0).0;
|
2020-12-10 07:54:15 -08:00
|
|
|
|
|
|
|
let num_packets = data_shreds.len();
|
2019-10-06 12:56:17 -07:00
|
|
|
|
2019-10-10 13:24:03 -07:00
|
|
|
let retransmitter_handles = retransmitter(
|
|
|
|
Arc::new(sockets),
|
|
|
|
bank_forks,
|
|
|
|
&leader_schedule_cache,
|
|
|
|
cluster_info,
|
|
|
|
packet_receiver,
|
2021-02-23 13:06:33 -08:00
|
|
|
&Arc::new(MaxSlots::default()),
|
2021-02-28 23:29:11 -08:00
|
|
|
None,
|
2019-10-10 13:24:03 -07:00
|
|
|
);
|
|
|
|
|
2020-12-10 07:54:15 -08:00
|
|
|
let mut index = 0;
|
|
|
|
let mut slot = 0;
|
2019-10-10 13:24:03 -07:00
|
|
|
let total = Arc::new(AtomicUsize::new(0));
|
2019-10-06 12:56:17 -07:00
|
|
|
bencher.iter(move || {
|
2019-10-10 13:24:03 -07:00
|
|
|
let peer_sockets1 = peer_sockets.clone();
|
|
|
|
let handles: Vec<_> = (0..NUM_PEERS)
|
|
|
|
.map(|p| {
|
|
|
|
let peer_sockets2 = peer_sockets1.clone();
|
|
|
|
let total2 = total.clone();
|
|
|
|
Builder::new()
|
|
|
|
.name("recv".to_string())
|
|
|
|
.spawn(move || {
|
|
|
|
info!("{} waiting on {:?}", p, peer_sockets2[p]);
|
|
|
|
let mut buf = [0u8; 1024];
|
|
|
|
loop {
|
|
|
|
while peer_sockets2[p].recv(&mut buf).is_ok() {
|
|
|
|
total2.fetch_add(1, Ordering::Relaxed);
|
|
|
|
}
|
2020-12-10 07:54:15 -08:00
|
|
|
if total2.load(Ordering::Relaxed) >= num_packets {
|
2019-10-10 13:24:03 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
info!("{} recv", total2.load(Ordering::Relaxed));
|
|
|
|
sleep(Duration::from_millis(1));
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.unwrap()
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
2020-12-10 07:54:15 -08:00
|
|
|
for shred in data_shreds.iter_mut() {
|
|
|
|
shred.set_slot(slot);
|
|
|
|
shred.set_index(index);
|
|
|
|
index += 1;
|
|
|
|
index %= 200;
|
|
|
|
let mut p = Packet::default();
|
|
|
|
shred.copy_to_packet(&mut p);
|
|
|
|
let _ = packet_sender.send(Packets::new(vec![p]));
|
2019-10-06 12:56:17 -07:00
|
|
|
}
|
2020-12-10 07:54:15 -08:00
|
|
|
slot += 1;
|
|
|
|
|
2019-10-06 12:56:17 -07:00
|
|
|
info!("sent...");
|
2019-10-10 13:24:03 -07:00
|
|
|
|
|
|
|
let mut join_time = Measure::start("join");
|
|
|
|
for h in handles {
|
|
|
|
h.join().unwrap();
|
|
|
|
}
|
|
|
|
join_time.stop();
|
|
|
|
info!("took: {}ms", join_time.as_ms());
|
|
|
|
|
|
|
|
total.store(0, Ordering::Relaxed);
|
2019-10-06 12:56:17 -07:00
|
|
|
});
|
2019-10-10 13:24:03 -07:00
|
|
|
|
|
|
|
for t in retransmitter_handles {
|
|
|
|
t.join().unwrap();
|
|
|
|
}
|
2019-10-06 12:56:17 -07:00
|
|
|
}
|