2018-11-14 12:06:06 -08:00
|
|
|
#![feature(test)]
|
2022-10-10 12:43:53 -07:00
|
|
|
#![cfg(feature = "sbf_c")]
|
2020-09-10 11:13:35 -07:00
|
|
|
|
2018-11-14 12:06:06 -08:00
|
|
|
extern crate test;
|
2020-09-10 11:13:35 -07:00
|
|
|
#[macro_use]
|
|
|
|
extern crate solana_bpf_loader_program;
|
2018-11-14 12:06:06 -08:00
|
|
|
|
2022-02-21 19:59:06 -08:00
|
|
|
use {
|
|
|
|
byteorder::{ByteOrder, LittleEndian, WriteBytesExt},
|
|
|
|
solana_bpf_loader_program::{
|
2022-10-06 11:31:58 -07:00
|
|
|
create_vm, serialization::serialize_parameters, syscalls::register_syscalls,
|
2022-02-21 19:59:06 -08:00
|
|
|
ThisInstructionMeter,
|
|
|
|
},
|
|
|
|
solana_measure::measure::Measure,
|
|
|
|
solana_program_runtime::invoke_context::with_mock_invoke_context,
|
|
|
|
solana_rbpf::{
|
2022-10-06 23:45:05 -07:00
|
|
|
ebpf::MM_INPUT_START,
|
2022-02-21 19:59:06 -08:00
|
|
|
elf::Executable,
|
2022-10-06 23:45:05 -07:00
|
|
|
memory_region::MemoryRegion,
|
2022-06-07 04:45:07 -07:00
|
|
|
verifier::RequisiteVerifier,
|
|
|
|
vm::{Config, InstructionMeter, SyscallRegistry, VerifiedExecutable},
|
2022-02-21 19:59:06 -08:00
|
|
|
},
|
|
|
|
solana_runtime::{
|
|
|
|
bank::Bank,
|
|
|
|
bank_client::BankClient,
|
|
|
|
genesis_utils::{create_genesis_config, GenesisConfigInfo},
|
|
|
|
loader_utils::load_program,
|
|
|
|
},
|
|
|
|
solana_sdk::{
|
|
|
|
bpf_loader,
|
|
|
|
client::SyncClient,
|
|
|
|
entrypoint::SUCCESS,
|
|
|
|
instruction::{AccountMeta, Instruction},
|
|
|
|
message::Message,
|
|
|
|
pubkey::Pubkey,
|
|
|
|
signature::{Keypair, Signer},
|
|
|
|
},
|
|
|
|
std::{env, fs::File, io::Read, mem, path::PathBuf, sync::Arc},
|
|
|
|
test::Bencher,
|
2020-11-24 09:00:19 -08:00
|
|
|
};
|
2018-11-14 12:06:06 -08:00
|
|
|
|
2022-10-10 12:43:53 -07:00
|
|
|
/// SBF program file extension
|
|
|
|
const PLATFORM_FILE_EXTENSION_SBF: &str = "so";
|
|
|
|
/// Create a SBF program file name
|
|
|
|
fn create_sbf_path(name: &str) -> PathBuf {
|
2018-11-14 12:06:06 -08:00
|
|
|
let mut pathbuf = {
|
|
|
|
let current_exe = env::current_exe().unwrap();
|
|
|
|
PathBuf::from(current_exe.parent().unwrap().parent().unwrap())
|
|
|
|
};
|
2022-10-10 12:43:53 -07:00
|
|
|
pathbuf.push("sbf/");
|
2018-11-14 12:06:06 -08:00
|
|
|
pathbuf.push(name);
|
2022-10-10 12:43:53 -07:00
|
|
|
pathbuf.set_extension(PLATFORM_FILE_EXTENSION_SBF);
|
2018-11-14 12:06:06 -08:00
|
|
|
pathbuf
|
|
|
|
}
|
|
|
|
|
2020-09-10 11:13:35 -07:00
|
|
|
fn load_elf(name: &str) -> Result<Vec<u8>, std::io::Error> {
|
2022-10-10 12:43:53 -07:00
|
|
|
let path = create_sbf_path(name);
|
2019-03-04 09:56:27 -08:00
|
|
|
let mut file = File::open(&path).expect(&format!("Unable to open {:?}", path));
|
|
|
|
let mut elf = Vec::new();
|
|
|
|
file.read_to_end(&mut elf).unwrap();
|
|
|
|
Ok(elf)
|
|
|
|
}
|
|
|
|
|
2022-10-10 12:43:53 -07:00
|
|
|
fn load_sbf_program(
|
2020-09-10 11:13:35 -07:00
|
|
|
bank_client: &BankClient,
|
|
|
|
loader_id: &Pubkey,
|
|
|
|
payer_keypair: &Keypair,
|
|
|
|
name: &str,
|
|
|
|
) -> Pubkey {
|
2022-10-10 12:43:53 -07:00
|
|
|
let path = create_sbf_path(name);
|
2020-09-10 11:13:35 -07:00
|
|
|
let mut file = File::open(path).unwrap();
|
|
|
|
let mut elf = Vec::new();
|
|
|
|
file.read_to_end(&mut elf).unwrap();
|
|
|
|
load_program(bank_client, payer_keypair, loader_id, elf)
|
|
|
|
}
|
|
|
|
|
2018-11-14 12:06:06 -08:00
|
|
|
const ARMSTRONG_LIMIT: u64 = 500;
|
|
|
|
const ARMSTRONG_EXPECTED: u64 = 5;
|
|
|
|
|
|
|
|
#[bench]
|
2020-09-14 17:42:37 -07:00
|
|
|
fn bench_program_create_executable(bencher: &mut Bencher) {
|
2020-09-10 11:13:35 -07:00
|
|
|
let elf = load_elf("bench_alu").unwrap();
|
2018-11-14 12:06:06 -08:00
|
|
|
|
|
|
|
bencher.iter(|| {
|
2022-10-06 11:31:58 -07:00
|
|
|
let _ = Executable::<ThisInstructionMeter>::from_elf(
|
2021-04-20 11:07:30 -07:00
|
|
|
&elf,
|
|
|
|
Config::default(),
|
2021-07-07 00:50:11 -07:00
|
|
|
SyscallRegistry::default(),
|
2021-04-20 11:07:30 -07:00
|
|
|
)
|
|
|
|
.unwrap();
|
2018-11-14 12:06:06 -08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_program_alu(bencher: &mut Bencher) {
|
|
|
|
let ns_per_s = 1000000000;
|
|
|
|
let one_million = 1000000;
|
|
|
|
let mut inner_iter = vec![];
|
|
|
|
inner_iter
|
|
|
|
.write_u64::<LittleEndian>(ARMSTRONG_LIMIT)
|
|
|
|
.unwrap();
|
2018-11-26 07:49:23 -08:00
|
|
|
inner_iter.write_u64::<LittleEndian>(0).unwrap();
|
2020-09-10 11:13:35 -07:00
|
|
|
let elf = load_elf("bench_alu").unwrap();
|
2021-11-04 13:47:32 -07:00
|
|
|
let loader_id = bpf_loader::id();
|
2022-10-04 14:44:22 -07:00
|
|
|
with_mock_invoke_context(loader_id, 10000001, false, |invoke_context| {
|
2021-12-03 03:15:22 -08:00
|
|
|
invoke_context
|
|
|
|
.get_compute_meter()
|
|
|
|
.borrow_mut()
|
|
|
|
.mock_set_remaining(std::i64::MAX as u64);
|
2022-10-06 11:31:58 -07:00
|
|
|
let executable = Executable::<ThisInstructionMeter>::from_elf(
|
2021-11-04 13:47:32 -07:00
|
|
|
&elf,
|
|
|
|
Config::default(),
|
2022-05-20 13:19:41 -07:00
|
|
|
register_syscalls(invoke_context, true).unwrap(),
|
2021-11-04 13:47:32 -07:00
|
|
|
)
|
|
|
|
.unwrap();
|
2022-06-07 04:45:07 -07:00
|
|
|
|
2022-10-06 11:31:58 -07:00
|
|
|
let mut verified_executable =
|
|
|
|
VerifiedExecutable::<RequisiteVerifier, ThisInstructionMeter>::from_executable(
|
|
|
|
executable,
|
|
|
|
)
|
|
|
|
.unwrap();
|
2022-06-07 04:45:07 -07:00
|
|
|
|
|
|
|
verified_executable.jit_compile().unwrap();
|
2021-11-04 13:47:32 -07:00
|
|
|
let compute_meter = invoke_context.get_compute_meter();
|
|
|
|
let mut instruction_meter = ThisInstructionMeter { compute_meter };
|
2022-06-07 04:45:07 -07:00
|
|
|
let mut vm = create_vm(
|
|
|
|
&verified_executable,
|
2022-10-06 23:45:05 -07:00
|
|
|
vec![MemoryRegion::new_writable(&mut inner_iter, MM_INPUT_START)],
|
2022-06-07 04:45:07 -07:00
|
|
|
vec![],
|
|
|
|
invoke_context,
|
|
|
|
)
|
|
|
|
.unwrap();
|
2018-11-26 07:49:23 -08:00
|
|
|
|
2021-11-04 13:47:32 -07:00
|
|
|
println!("Interpreted:");
|
|
|
|
assert_eq!(
|
|
|
|
SUCCESS,
|
|
|
|
vm.execute_program_interpreted(&mut instruction_meter)
|
|
|
|
.unwrap()
|
|
|
|
);
|
|
|
|
assert_eq!(ARMSTRONG_LIMIT, LittleEndian::read_u64(&inner_iter));
|
|
|
|
assert_eq!(
|
|
|
|
ARMSTRONG_EXPECTED,
|
|
|
|
LittleEndian::read_u64(&inner_iter[mem::size_of::<u64>()..])
|
|
|
|
);
|
|
|
|
|
|
|
|
bencher.iter(|| {
|
|
|
|
vm.execute_program_interpreted(&mut instruction_meter)
|
|
|
|
.unwrap();
|
|
|
|
});
|
|
|
|
let instructions = vm.get_total_instruction_count();
|
|
|
|
let summary = bencher.bench(|_bencher| {}).unwrap();
|
|
|
|
println!(" {:?} instructions", instructions);
|
|
|
|
println!(" {:?} ns/iter median", summary.median as u64);
|
|
|
|
assert!(0f64 != summary.median);
|
|
|
|
let mips = (instructions * (ns_per_s / summary.median as u64)) / one_million;
|
|
|
|
println!(" {:?} MIPS", mips);
|
|
|
|
println!("{{ \"type\": \"bench\", \"name\": \"bench_program_alu_interpreted_mips\", \"median\": {:?}, \"deviation\": 0 }}", mips);
|
|
|
|
|
|
|
|
println!("JIT to native:");
|
|
|
|
assert_eq!(
|
|
|
|
SUCCESS,
|
|
|
|
vm.execute_program_jit(&mut instruction_meter).unwrap()
|
|
|
|
);
|
|
|
|
assert_eq!(ARMSTRONG_LIMIT, LittleEndian::read_u64(&inner_iter));
|
|
|
|
assert_eq!(
|
|
|
|
ARMSTRONG_EXPECTED,
|
|
|
|
LittleEndian::read_u64(&inner_iter[mem::size_of::<u64>()..])
|
|
|
|
);
|
|
|
|
|
|
|
|
bencher.iter(|| vm.execute_program_jit(&mut instruction_meter).unwrap());
|
|
|
|
let summary = bencher.bench(|_bencher| {}).unwrap();
|
|
|
|
println!(" {:?} instructions", instructions);
|
|
|
|
println!(" {:?} ns/iter median", summary.median as u64);
|
|
|
|
assert!(0f64 != summary.median);
|
|
|
|
let mips = (instructions * (ns_per_s / summary.median as u64)) / one_million;
|
|
|
|
println!(" {:?} MIPS", mips);
|
|
|
|
println!("{{ \"type\": \"bench\", \"name\": \"bench_program_alu_jit_to_native_mips\", \"median\": {:?}, \"deviation\": 0 }}", mips);
|
2018-11-14 12:06:06 -08:00
|
|
|
});
|
|
|
|
}
|
2020-04-28 14:33:56 -07:00
|
|
|
|
2020-09-10 11:13:35 -07:00
|
|
|
#[bench]
|
|
|
|
fn bench_program_execute_noop(bencher: &mut Bencher) {
|
|
|
|
let GenesisConfigInfo {
|
|
|
|
genesis_config,
|
|
|
|
mint_keypair,
|
|
|
|
..
|
|
|
|
} = create_genesis_config(50);
|
2021-08-04 15:30:43 -07:00
|
|
|
let mut bank = Bank::new_for_benches(&genesis_config);
|
2020-09-10 11:13:35 -07:00
|
|
|
let (name, id, entrypoint) = solana_bpf_loader_program!();
|
2021-09-28 23:25:08 -07:00
|
|
|
bank.add_builtin(&name, &id, entrypoint);
|
2020-09-10 11:13:35 -07:00
|
|
|
let bank = Arc::new(bank);
|
|
|
|
let bank_client = BankClient::new_shared(&bank);
|
|
|
|
|
|
|
|
let invoke_program_id =
|
2022-10-10 12:43:53 -07:00
|
|
|
load_sbf_program(&bank_client, &bpf_loader::id(), &mint_keypair, "noop");
|
2020-09-10 11:13:35 -07:00
|
|
|
|
|
|
|
let mint_pubkey = mint_keypair.pubkey();
|
|
|
|
let account_metas = vec![AccountMeta::new(mint_pubkey, true)];
|
|
|
|
|
2021-03-04 12:34:36 -08:00
|
|
|
let instruction =
|
|
|
|
Instruction::new_with_bincode(invoke_program_id, &[u8::MAX, 0, 0, 0], account_metas);
|
2020-09-10 11:13:35 -07:00
|
|
|
let message = Message::new(&[instruction], Some(&mint_pubkey));
|
|
|
|
|
|
|
|
bank_client
|
|
|
|
.send_and_confirm_message(&[&mint_keypair], message.clone())
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
bencher.iter(|| {
|
|
|
|
bank.clear_signatures();
|
|
|
|
bank_client
|
|
|
|
.send_and_confirm_message(&[&mint_keypair], message.clone())
|
|
|
|
.unwrap();
|
|
|
|
});
|
2021-10-19 21:01:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_create_vm(bencher: &mut Bencher) {
|
|
|
|
let elf = load_elf("noop").unwrap();
|
2021-11-04 13:47:32 -07:00
|
|
|
let loader_id = bpf_loader::id();
|
2022-10-04 14:44:22 -07:00
|
|
|
with_mock_invoke_context(loader_id, 10000001, false, |invoke_context| {
|
2021-11-04 13:47:32 -07:00
|
|
|
const BUDGET: u64 = 200_000;
|
2021-12-03 03:15:22 -08:00
|
|
|
invoke_context
|
|
|
|
.get_compute_meter()
|
|
|
|
.borrow_mut()
|
|
|
|
.mock_set_remaining(BUDGET);
|
2021-11-04 13:47:32 -07:00
|
|
|
|
|
|
|
// Serialize account data
|
2022-10-06 23:45:05 -07:00
|
|
|
let (_serialized, regions, account_lengths) = serialize_parameters(
|
2022-01-03 14:30:56 -08:00
|
|
|
invoke_context.transaction_context,
|
2022-01-04 18:15:15 -08:00
|
|
|
invoke_context
|
|
|
|
.transaction_context
|
|
|
|
.get_current_instruction_context()
|
|
|
|
.unwrap(),
|
2022-07-20 05:12:43 -07:00
|
|
|
true, // should_cap_ix_accounts
|
2021-11-04 13:47:32 -07:00
|
|
|
)
|
|
|
|
.unwrap();
|
2021-10-19 21:01:39 -07:00
|
|
|
|
2022-10-06 11:31:58 -07:00
|
|
|
let executable = Executable::<ThisInstructionMeter>::from_elf(
|
2021-11-04 13:47:32 -07:00
|
|
|
&elf,
|
|
|
|
Config::default(),
|
2022-05-20 13:19:41 -07:00
|
|
|
register_syscalls(invoke_context, true).unwrap(),
|
2021-10-19 21:01:39 -07:00
|
|
|
)
|
|
|
|
.unwrap();
|
2021-11-04 13:47:32 -07:00
|
|
|
|
2022-10-06 11:31:58 -07:00
|
|
|
let verified_executable =
|
|
|
|
VerifiedExecutable::<RequisiteVerifier, ThisInstructionMeter>::from_executable(
|
|
|
|
executable,
|
|
|
|
)
|
|
|
|
.unwrap();
|
2022-06-07 04:45:07 -07:00
|
|
|
|
2021-11-04 13:47:32 -07:00
|
|
|
bencher.iter(|| {
|
2022-05-19 15:14:28 -07:00
|
|
|
let _ = create_vm(
|
2022-06-07 04:45:07 -07:00
|
|
|
&verified_executable,
|
2022-10-06 23:45:05 -07:00
|
|
|
regions.clone(),
|
2022-05-19 15:14:28 -07:00
|
|
|
account_lengths.clone(),
|
|
|
|
invoke_context,
|
|
|
|
)
|
|
|
|
.unwrap();
|
2021-11-04 13:47:32 -07:00
|
|
|
});
|
2021-10-19 21:01:39 -07:00
|
|
|
});
|
2020-09-10 11:13:35 -07:00
|
|
|
}
|
|
|
|
|
2020-09-25 09:01:22 -07:00
|
|
|
#[bench]
|
|
|
|
fn bench_instruction_count_tuner(_bencher: &mut Bencher) {
|
2020-11-04 09:46:26 -08:00
|
|
|
let elf = load_elf("tuner").unwrap();
|
2021-11-04 13:47:32 -07:00
|
|
|
let loader_id = bpf_loader::id();
|
2022-10-04 14:44:22 -07:00
|
|
|
with_mock_invoke_context(loader_id, 10000001, true, |invoke_context| {
|
2021-11-04 13:47:32 -07:00
|
|
|
const BUDGET: u64 = 200_000;
|
2021-12-03 03:15:22 -08:00
|
|
|
invoke_context
|
|
|
|
.get_compute_meter()
|
|
|
|
.borrow_mut()
|
|
|
|
.mock_set_remaining(BUDGET);
|
2021-11-04 13:47:32 -07:00
|
|
|
|
|
|
|
// Serialize account data
|
2022-10-06 23:45:05 -07:00
|
|
|
let (_serialized, regions, account_lengths) = serialize_parameters(
|
2022-01-03 14:30:56 -08:00
|
|
|
invoke_context.transaction_context,
|
2022-01-04 18:15:15 -08:00
|
|
|
invoke_context
|
|
|
|
.transaction_context
|
|
|
|
.get_current_instruction_context()
|
|
|
|
.unwrap(),
|
2022-07-20 05:12:43 -07:00
|
|
|
true, // should_cap_ix_accounts
|
2021-11-04 13:47:32 -07:00
|
|
|
)
|
|
|
|
.unwrap();
|
2020-11-04 09:46:26 -08:00
|
|
|
|
2022-10-06 11:31:58 -07:00
|
|
|
let executable = Executable::<ThisInstructionMeter>::from_elf(
|
2021-11-04 13:47:32 -07:00
|
|
|
&elf,
|
|
|
|
Config::default(),
|
2022-05-20 13:19:41 -07:00
|
|
|
register_syscalls(invoke_context, true).unwrap(),
|
2021-11-04 13:47:32 -07:00
|
|
|
)
|
|
|
|
.unwrap();
|
2022-06-07 04:45:07 -07:00
|
|
|
|
2022-10-06 11:31:58 -07:00
|
|
|
let verified_executable =
|
|
|
|
VerifiedExecutable::<RequisiteVerifier, ThisInstructionMeter>::from_executable(
|
|
|
|
executable,
|
|
|
|
)
|
|
|
|
.unwrap();
|
2022-06-07 04:45:07 -07:00
|
|
|
|
2021-12-01 23:58:02 -08:00
|
|
|
let compute_meter = invoke_context.get_compute_meter();
|
2021-11-04 13:47:32 -07:00
|
|
|
let mut instruction_meter = ThisInstructionMeter { compute_meter };
|
2022-05-19 15:14:28 -07:00
|
|
|
let mut vm = create_vm(
|
2022-06-07 04:45:07 -07:00
|
|
|
&verified_executable,
|
2022-10-06 23:45:05 -07:00
|
|
|
regions,
|
2022-05-19 15:14:28 -07:00
|
|
|
account_lengths,
|
|
|
|
invoke_context,
|
|
|
|
)
|
|
|
|
.unwrap();
|
2020-11-04 09:46:26 -08:00
|
|
|
|
2021-11-04 13:47:32 -07:00
|
|
|
let mut measure = Measure::start("tune");
|
|
|
|
let _ = vm.execute_program_interpreted(&mut instruction_meter);
|
|
|
|
measure.stop();
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
0,
|
|
|
|
instruction_meter.get_remaining(),
|
|
|
|
"Tuner must consume the whole budget"
|
|
|
|
);
|
|
|
|
println!(
|
|
|
|
"{:?} compute units took {:?} us ({:?} instructions)",
|
|
|
|
BUDGET - instruction_meter.get_remaining(),
|
|
|
|
measure.as_us(),
|
|
|
|
vm.get_total_instruction_count(),
|
|
|
|
);
|
|
|
|
});
|
2020-09-25 09:01:22 -07:00
|
|
|
}
|