2018-11-14 12:06:06 -08:00
|
|
|
#![feature(test)]
|
|
|
|
#![cfg(feature = "bpf_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
|
|
|
|
2018-11-26 07:49:23 -08:00
|
|
|
use byteorder::{ByteOrder, LittleEndian, WriteBytesExt};
|
2020-11-24 09:00:19 -08:00
|
|
|
use solana_bpf_loader_program::{
|
2021-02-18 23:42:09 -08:00
|
|
|
create_vm, serialization::serialize_parameters, syscalls::register_syscalls, BpfError,
|
2020-11-24 09:00:19 -08:00
|
|
|
ThisInstructionMeter,
|
|
|
|
};
|
2020-09-25 09:01:22 -07:00
|
|
|
use solana_measure::measure::Measure;
|
2021-07-07 00:50:11 -07:00
|
|
|
use solana_rbpf::vm::{Config, Executable, InstructionMeter, SyscallRegistry};
|
2020-09-10 11:13:35 -07:00
|
|
|
use solana_runtime::{
|
|
|
|
bank::Bank,
|
|
|
|
bank_client::BankClient,
|
|
|
|
genesis_utils::{create_genesis_config, GenesisConfigInfo},
|
|
|
|
loader_utils::load_program,
|
|
|
|
};
|
2020-04-28 14:33:56 -07:00
|
|
|
use solana_sdk::{
|
2021-03-09 13:06:07 -08:00
|
|
|
account::AccountSharedData,
|
2020-09-14 17:42:37 -07:00
|
|
|
bpf_loader,
|
2020-09-10 11:13:35 -07:00
|
|
|
client::SyncClient,
|
|
|
|
entrypoint::SUCCESS,
|
2020-10-29 02:04:47 -07:00
|
|
|
instruction::{AccountMeta, Instruction},
|
2020-04-28 14:33:56 -07:00
|
|
|
message::Message,
|
2020-11-04 09:46:26 -08:00
|
|
|
process_instruction::{InvokeContext, MockInvokeContext},
|
2020-04-28 14:33:56 -07:00
|
|
|
pubkey::Pubkey,
|
2020-09-10 11:13:35 -07:00
|
|
|
signature::{Keypair, Signer},
|
2020-04-28 14:33:56 -07:00
|
|
|
};
|
2020-11-04 09:46:26 -08:00
|
|
|
use std::{cell::RefCell, env, fs::File, io::Read, mem, path::PathBuf, sync::Arc};
|
2018-11-14 12:06:06 -08:00
|
|
|
use test::Bencher;
|
|
|
|
|
|
|
|
/// BPF program file extension
|
2018-12-04 22:03:32 -08:00
|
|
|
const PLATFORM_FILE_EXTENSION_BPF: &str = "so";
|
2018-11-14 12:06:06 -08:00
|
|
|
/// Create a BPF program file name
|
|
|
|
fn create_bpf_path(name: &str) -> PathBuf {
|
|
|
|
let mut pathbuf = {
|
|
|
|
let current_exe = env::current_exe().unwrap();
|
|
|
|
PathBuf::from(current_exe.parent().unwrap().parent().unwrap())
|
|
|
|
};
|
|
|
|
pathbuf.push("bpf/");
|
|
|
|
pathbuf.push(name);
|
|
|
|
pathbuf.set_extension(PLATFORM_FILE_EXTENSION_BPF);
|
|
|
|
pathbuf
|
|
|
|
}
|
|
|
|
|
2020-09-10 11:13:35 -07:00
|
|
|
fn load_elf(name: &str) -> Result<Vec<u8>, std::io::Error> {
|
|
|
|
let path = create_bpf_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)
|
|
|
|
}
|
|
|
|
|
2020-09-10 11:13:35 -07:00
|
|
|
fn load_bpf_program(
|
|
|
|
bank_client: &BankClient,
|
|
|
|
loader_id: &Pubkey,
|
|
|
|
payer_keypair: &Keypair,
|
|
|
|
name: &str,
|
|
|
|
) -> Pubkey {
|
|
|
|
let path = create_bpf_path(name);
|
|
|
|
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(|| {
|
2021-04-20 11:07:30 -07:00
|
|
|
let _ = <dyn Executable<BpfError, ThisInstructionMeter>>::from_elf(
|
|
|
|
&elf,
|
|
|
|
None,
|
|
|
|
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-11 11:07:03 -07:00
|
|
|
let loader_id = bpf_loader::id();
|
2021-04-19 09:48:48 -07:00
|
|
|
let mut invoke_context = MockInvokeContext::new(vec![]);
|
2018-11-14 12:06:06 -08:00
|
|
|
|
2020-09-10 11:13:35 -07:00
|
|
|
let elf = load_elf("bench_alu").unwrap();
|
2021-07-07 00:50:11 -07:00
|
|
|
let mut executable = <dyn Executable<BpfError, ThisInstructionMeter>>::from_elf(
|
|
|
|
&elf,
|
|
|
|
None,
|
|
|
|
Config::default(),
|
|
|
|
register_syscalls(&mut invoke_context).unwrap(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2020-11-24 09:00:19 -08:00
|
|
|
executable.jit_compile().unwrap();
|
2020-11-04 09:46:26 -08:00
|
|
|
let compute_meter = invoke_context.get_compute_meter();
|
|
|
|
let mut instruction_meter = ThisInstructionMeter { compute_meter };
|
2020-11-24 09:00:19 -08:00
|
|
|
let mut vm = create_vm(
|
2020-09-14 17:42:37 -07:00
|
|
|
&loader_id,
|
|
|
|
executable.as_ref(),
|
2020-11-04 09:46:26 -08:00
|
|
|
&mut inner_iter,
|
2020-09-14 17:42:37 -07:00
|
|
|
&mut invoke_context,
|
|
|
|
)
|
|
|
|
.unwrap();
|
2018-11-14 12:06:06 -08:00
|
|
|
|
|
|
|
println!("Interpreted:");
|
|
|
|
assert_eq!(
|
2020-09-10 11:13:35 -07:00
|
|
|
SUCCESS,
|
2020-11-04 09:46:26 -08:00
|
|
|
vm.execute_program_interpreted(&mut instruction_meter)
|
|
|
|
.unwrap()
|
2018-11-14 12:06:06 -08:00
|
|
|
);
|
2018-11-26 07:49:23 -08:00
|
|
|
assert_eq!(ARMSTRONG_LIMIT, LittleEndian::read_u64(&inner_iter));
|
|
|
|
assert_eq!(
|
|
|
|
ARMSTRONG_EXPECTED,
|
|
|
|
LittleEndian::read_u64(&inner_iter[mem::size_of::<u64>()..])
|
|
|
|
);
|
|
|
|
|
2018-11-14 12:06:06 -08:00
|
|
|
bencher.iter(|| {
|
2020-11-04 09:46:26 -08:00
|
|
|
vm.execute_program_interpreted(&mut instruction_meter)
|
|
|
|
.unwrap();
|
2018-11-14 12:06:06 -08:00
|
|
|
});
|
2020-08-21 15:31:19 -07:00
|
|
|
let instructions = vm.get_total_instruction_count();
|
2018-11-14 12:06:06 -08:00
|
|
|
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);
|
2018-11-21 12:16:16 -08:00
|
|
|
println!("{{ \"type\": \"bench\", \"name\": \"bench_program_alu_interpreted_mips\", \"median\": {:?}, \"deviation\": 0 }}", mips);
|
2018-11-14 12:06:06 -08:00
|
|
|
|
2020-11-04 09:46:26 -08:00
|
|
|
println!("JIT to native:");
|
2020-11-24 09:00:19 -08:00
|
|
|
assert_eq!(
|
|
|
|
SUCCESS,
|
|
|
|
vm.execute_program_jit(&mut instruction_meter).unwrap()
|
|
|
|
);
|
2020-11-04 09:46:26 -08:00
|
|
|
assert_eq!(ARMSTRONG_LIMIT, LittleEndian::read_u64(&inner_iter));
|
|
|
|
assert_eq!(
|
|
|
|
ARMSTRONG_EXPECTED,
|
|
|
|
LittleEndian::read_u64(&inner_iter[mem::size_of::<u64>()..])
|
|
|
|
);
|
2018-11-26 07:49:23 -08:00
|
|
|
|
2020-11-24 09:00:19 -08:00
|
|
|
bencher.iter(|| vm.execute_program_jit(&mut instruction_meter).unwrap());
|
2020-11-04 09:46:26 -08:00
|
|
|
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!();
|
2020-10-29 16:17:36 -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 =
|
|
|
|
load_bpf_program(&bank_client, &bpf_loader::id(), &mint_keypair, "noop");
|
|
|
|
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-09-25 09:01:22 -07:00
|
|
|
#[bench]
|
|
|
|
fn bench_instruction_count_tuner(_bencher: &mut Bencher) {
|
|
|
|
const BUDGET: u64 = 200_000;
|
|
|
|
let loader_id = bpf_loader::id();
|
2020-10-13 12:16:27 -07:00
|
|
|
|
2021-03-09 13:06:07 -08:00
|
|
|
let accounts = [RefCell::new(AccountSharedData::new(
|
2020-10-28 22:01:07 -07:00
|
|
|
1,
|
|
|
|
10000001,
|
|
|
|
&solana_sdk::pubkey::new_rand(),
|
|
|
|
))];
|
2020-10-19 12:12:08 -07:00
|
|
|
let keys = [solana_sdk::pubkey::new_rand()];
|
2020-10-13 12:16:27 -07:00
|
|
|
let keyed_accounts: Vec<_> = keys
|
|
|
|
.iter()
|
|
|
|
.zip(&accounts)
|
2020-10-26 09:14:57 -07:00
|
|
|
.map(|(key, account)| solana_sdk::keyed_account::KeyedAccount::new(&key, false, &account))
|
2020-10-13 12:16:27 -07:00
|
|
|
.collect();
|
|
|
|
let instruction_data = vec![0u8];
|
|
|
|
|
2021-04-19 09:48:48 -07:00
|
|
|
let mut invoke_context = MockInvokeContext::new(keyed_accounts);
|
|
|
|
invoke_context.compute_meter.remaining = BUDGET;
|
|
|
|
|
2020-10-13 12:16:27 -07:00
|
|
|
// Serialize account data
|
2021-04-19 09:48:48 -07:00
|
|
|
let keyed_accounts = invoke_context.get_keyed_accounts().unwrap();
|
2020-11-24 09:00:19 -08:00
|
|
|
let mut serialized = serialize_parameters(
|
2020-10-13 12:16:27 -07:00
|
|
|
&bpf_loader::id(),
|
2020-10-19 12:12:08 -07:00
|
|
|
&solana_sdk::pubkey::new_rand(),
|
2021-04-19 09:48:48 -07:00
|
|
|
keyed_accounts,
|
2020-10-13 12:16:27 -07:00
|
|
|
&instruction_data,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
2020-11-04 09:46:26 -08:00
|
|
|
let elf = load_elf("tuner").unwrap();
|
2021-07-07 00:50:11 -07:00
|
|
|
let executable = <dyn Executable<BpfError, ThisInstructionMeter>>::from_elf(
|
|
|
|
&elf,
|
|
|
|
None,
|
|
|
|
Config::default(),
|
|
|
|
register_syscalls(&mut invoke_context).unwrap(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2020-11-04 09:46:26 -08:00
|
|
|
let compute_meter = invoke_context.get_compute_meter();
|
|
|
|
let mut instruction_meter = ThisInstructionMeter { compute_meter };
|
2020-11-24 09:00:19 -08:00
|
|
|
let mut vm = create_vm(
|
2020-11-04 09:46:26 -08:00
|
|
|
&loader_id,
|
|
|
|
executable.as_ref(),
|
2021-04-20 11:07:30 -07:00
|
|
|
serialized.as_slice_mut(),
|
2020-11-04 09:46:26 -08:00
|
|
|
&mut invoke_context,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let mut measure = Measure::start("tune");
|
|
|
|
let _ = vm.execute_program_interpreted(&mut instruction_meter);
|
2020-10-28 22:01:07 -07:00
|
|
|
measure.stop();
|
2020-11-04 09:46:26 -08:00
|
|
|
|
2020-09-25 09:01:22 -07:00
|
|
|
assert_eq!(
|
|
|
|
0,
|
|
|
|
instruction_meter.get_remaining(),
|
|
|
|
"Tuner must consume the whole budget"
|
|
|
|
);
|
|
|
|
println!(
|
2020-09-29 14:36:30 -07:00
|
|
|
"{:?} compute units took {:?} us ({:?} instructions)",
|
2020-09-25 09:01:22 -07:00
|
|
|
BUDGET - instruction_meter.get_remaining(),
|
|
|
|
measure.as_us(),
|
|
|
|
vm.get_total_instruction_count(),
|
|
|
|
);
|
|
|
|
}
|