Validator CLI option to enable just-in-time compilation of BPF (#13789)

* Adds a CLI option to the validator to enable just-in-time compilation of BPF.

* Refactoring to use bpf_loader_program instead of feature_set to pass JIT flag from the validator CLI to the executor.
This commit is contained in:
Alexander Meißner
2020-12-07 09:49:55 +01:00
committed by GitHub
parent 3425e98a6b
commit a706706572
9 changed files with 93 additions and 31 deletions

View File

@ -105,6 +105,7 @@ pub struct ValidatorConfig {
pub require_tower: bool, pub require_tower: bool,
pub debug_keys: Option<Arc<HashSet<Pubkey>>>, pub debug_keys: Option<Arc<HashSet<Pubkey>>>,
pub contact_debug_interval: u64, pub contact_debug_interval: u64,
pub bpf_jit: bool,
} }
impl Default for ValidatorConfig { impl Default for ValidatorConfig {
@ -141,6 +142,7 @@ impl Default for ValidatorConfig {
require_tower: false, require_tower: false,
debug_keys: None, debug_keys: None,
contact_debug_interval: DEFAULT_CONTACT_DEBUG_INTERVAL, contact_debug_interval: DEFAULT_CONTACT_DEBUG_INTERVAL,
bpf_jit: false,
} }
} }
} }
@ -857,6 +859,7 @@ fn new_banks_from_ledger(
} }
let process_options = blockstore_processor::ProcessOptions { let process_options = blockstore_processor::ProcessOptions {
bpf_jit: config.bpf_jit,
poh_verify, poh_verify,
dev_halt_at_slot: config.dev_halt_at_slot, dev_halt_at_slot: config.dev_halt_at_slot,
new_hard_forks: config.new_hard_forks.clone(), new_hard_forks: config.new_hard_forks.clone(),

View File

@ -66,7 +66,10 @@ pub fn load(
compression, compression,
genesis_config, genesis_config,
process_options.debug_keys.clone(), process_options.debug_keys.clone(),
Some(&crate::builtins::get(genesis_config.cluster_type)), Some(&crate::builtins::get(
genesis_config.cluster_type,
process_options.bpf_jit,
)),
) )
.expect("Load from snapshot failed"); .expect("Load from snapshot failed");

View File

@ -311,6 +311,7 @@ pub type ProcessCallback = Arc<dyn Fn(&Bank) + Sync + Send>;
#[derive(Default, Clone)] #[derive(Default, Clone)]
pub struct ProcessOptions { pub struct ProcessOptions {
pub bpf_jit: bool,
pub poh_verify: bool, pub poh_verify: bool,
pub full_leader_cache: bool, pub full_leader_cache: bool,
pub dev_halt_at_slot: Option<Slot>, pub dev_halt_at_slot: Option<Slot>,
@ -342,7 +343,10 @@ pub fn process_blockstore(
account_paths, account_paths,
&opts.frozen_accounts, &opts.frozen_accounts,
opts.debug_keys.clone(), opts.debug_keys.clone(),
Some(&crate::builtins::get(genesis_config.cluster_type)), Some(&crate::builtins::get(
genesis_config.cluster_type,
opts.bpf_jit,
)),
); );
let bank0 = Arc::new(bank0); let bank0 = Arc::new(bank0);
info!("processing ledger for slot 0..."); info!("processing ledger for slot 0...");

View File

@ -4,42 +4,42 @@ use solana_runtime::{
}; };
use solana_sdk::{feature_set, genesis_config::ClusterType, pubkey::Pubkey}; use solana_sdk::{feature_set, genesis_config::ClusterType, pubkey::Pubkey};
macro_rules! to_builtin {
($b:expr) => {
Builtin::new(&$b.0, $b.1, $b.2)
};
}
/// Builtin programs that are always available /// Builtin programs that are always available
fn genesis_builtins(cluster_type: ClusterType) -> Vec<Builtin> { fn genesis_builtins(cluster_type: ClusterType, bpf_jit: bool) -> Vec<Builtin> {
let builtins = if cluster_type != ClusterType::MainnetBeta { if cluster_type != ClusterType::MainnetBeta {
vec![ vec![
solana_bpf_loader_deprecated_program!(), to_builtin!(solana_bpf_loader_deprecated_program!()),
solana_bpf_loader_program!(), if bpf_jit {
to_builtin!(solana_bpf_loader_program_with_jit!())
} else {
to_builtin!(solana_bpf_loader_program!())
},
] ]
} else { } else {
// Remove this `else` block and the `cluster_type` argument to this function once // Remove this `else` block and the `cluster_type` argument to this function once
// `feature_set::bpf_loader2_program::id()` is active on Mainnet Beta // `feature_set::bpf_loader2_program::id()` is active on Mainnet Beta
vec![solana_bpf_loader_deprecated_program!()] vec![to_builtin!(solana_bpf_loader_deprecated_program!())]
}; }
builtins
.into_iter()
.map(|b| Builtin::new(&b.0, b.1, b.2))
.collect()
} }
/// Builtin programs activated dynamically by feature /// Builtin programs activated dynamically by feature
fn feature_builtins() -> Vec<(Builtin, Pubkey, ActivationType)> { fn feature_builtins() -> Vec<(Builtin, Pubkey, ActivationType)> {
let builtins = vec![( vec![(
solana_bpf_loader_program!(), to_builtin!(solana_bpf_loader_program!()),
feature_set::bpf_loader2_program::id(), feature_set::bpf_loader2_program::id(),
ActivationType::NewProgram, ActivationType::NewProgram,
)]; )]
builtins
.into_iter()
.map(|(b, p, t)| (Builtin::new(&b.0, b.1, b.2), p, t))
.collect()
} }
pub(crate) fn get(cluster_type: ClusterType) -> Builtins { pub(crate) fn get(cluster_type: ClusterType, bpf_jit: bool) -> Builtins {
Builtins { Builtins {
genesis_builtins: genesis_builtins(cluster_type), genesis_builtins: genesis_builtins(cluster_type, bpf_jit),
feature_builtins: feature_builtins(), feature_builtins: feature_builtins(),
} }
} }

View File

@ -4,6 +4,7 @@ pub mod bpf_verifier;
pub mod deprecated; pub mod deprecated;
pub mod serialization; pub mod serialization;
pub mod syscalls; pub mod syscalls;
pub mod with_jit;
use crate::{ use crate::{
bpf_verifier::VerifierError, bpf_verifier::VerifierError,
@ -91,11 +92,10 @@ fn map_ebpf_error(
InstructionError::InvalidAccountData InstructionError::InvalidAccountData
} }
const IS_JIT_ENABLED: bool = false;
pub fn create_and_cache_executor( pub fn create_and_cache_executor(
program: &KeyedAccount, program: &KeyedAccount,
invoke_context: &mut dyn InvokeContext, invoke_context: &mut dyn InvokeContext,
use_jit: bool,
) -> Result<Arc<BPFExecutor>, InstructionError> { ) -> Result<Arc<BPFExecutor>, InstructionError> {
let bpf_compute_budget = invoke_context.get_bpf_compute_budget(); let bpf_compute_budget = invoke_context.get_bpf_compute_budget();
let mut executable = Executable::<BPFError, ThisInstructionMeter>::from_elf( let mut executable = Executable::<BPFError, ThisInstructionMeter>::from_elf(
@ -120,7 +120,7 @@ pub fn create_and_cache_executor(
let syscall_registry = syscalls::register_syscalls(invoke_context) let syscall_registry = syscalls::register_syscalls(invoke_context)
.map_err(|e| map_ebpf_error(invoke_context, e))?; .map_err(|e| map_ebpf_error(invoke_context, e))?;
executable.set_syscall_registry(syscall_registry); executable.set_syscall_registry(syscall_registry);
if IS_JIT_ENABLED && executable.jit_compile().is_err() { if use_jit && executable.jit_compile().is_err() {
return Err(BPFLoaderError::JustInTimeCompilationFailed.into()); return Err(BPFLoaderError::JustInTimeCompilationFailed.into());
} }
let executor = Arc::new(BPFExecutor { executable }); let executor = Arc::new(BPFExecutor { executable });
@ -153,11 +153,12 @@ pub fn create_vm<'a>(
Ok(vm) Ok(vm)
} }
pub fn process_instruction( fn process_instruction_general(
program_id: &Pubkey, program_id: &Pubkey,
keyed_accounts: &[KeyedAccount], keyed_accounts: &[KeyedAccount],
instruction_data: &[u8], instruction_data: &[u8],
invoke_context: &mut dyn InvokeContext, invoke_context: &mut dyn InvokeContext,
use_jit: bool,
) -> Result<(), InstructionError> { ) -> Result<(), InstructionError> {
debug_assert!(bpf_loader::check_id(program_id) || bpf_loader_deprecated::check_id(program_id)); debug_assert!(bpf_loader::check_id(program_id) || bpf_loader_deprecated::check_id(program_id));
@ -172,9 +173,15 @@ pub fn process_instruction(
if is_executable(keyed_accounts)? { if is_executable(keyed_accounts)? {
let executor = match invoke_context.get_executor(program.unsigned_key()) { let executor = match invoke_context.get_executor(program.unsigned_key()) {
Some(executor) => executor, Some(executor) => executor,
None => create_and_cache_executor(program, invoke_context)?, None => create_and_cache_executor(program, invoke_context, use_jit)?,
}; };
executor.execute(program_id, keyed_accounts, instruction_data, invoke_context)? executor.execute(
program_id,
keyed_accounts,
instruction_data,
invoke_context,
use_jit,
)?
} else if !keyed_accounts.is_empty() { } else if !keyed_accounts.is_empty() {
match limited_deserialize(instruction_data)? { match limited_deserialize(instruction_data)? {
LoaderInstruction::Write { offset, bytes } => { LoaderInstruction::Write { offset, bytes } => {
@ -201,7 +208,7 @@ pub fn process_instruction(
return Err(InstructionError::MissingRequiredSignature); return Err(InstructionError::MissingRequiredSignature);
} }
let _ = create_and_cache_executor(program, invoke_context)?; let _ = create_and_cache_executor(program, invoke_context, use_jit)?;
program.try_account_ref_mut()?.executable = true; program.try_account_ref_mut()?.executable = true;
log!( log!(
logger, logger,
@ -214,6 +221,36 @@ pub fn process_instruction(
Ok(()) Ok(())
} }
pub fn process_instruction(
program_id: &Pubkey,
keyed_accounts: &[KeyedAccount],
instruction_data: &[u8],
invoke_context: &mut dyn InvokeContext,
) -> Result<(), InstructionError> {
process_instruction_general(
program_id,
keyed_accounts,
instruction_data,
invoke_context,
false,
)
}
pub fn process_instruction_jit(
program_id: &Pubkey,
keyed_accounts: &[KeyedAccount],
instruction_data: &[u8],
invoke_context: &mut dyn InvokeContext,
) -> Result<(), InstructionError> {
process_instruction_general(
program_id,
keyed_accounts,
instruction_data,
invoke_context,
true,
)
}
/// Passed to the VM to enforce the compute budget /// Passed to the VM to enforce the compute budget
pub struct ThisInstructionMeter { pub struct ThisInstructionMeter {
pub compute_meter: Rc<RefCell<dyn ComputeMeter>>, pub compute_meter: Rc<RefCell<dyn ComputeMeter>>,
@ -253,6 +290,7 @@ impl Executor for BPFExecutor {
keyed_accounts: &[KeyedAccount], keyed_accounts: &[KeyedAccount],
instruction_data: &[u8], instruction_data: &[u8],
invoke_context: &mut dyn InvokeContext, invoke_context: &mut dyn InvokeContext,
use_jit: bool,
) -> Result<(), InstructionError> { ) -> Result<(), InstructionError> {
let logger = invoke_context.get_logger(); let logger = invoke_context.get_logger();
let invoke_depth = invoke_context.invoke_depth(); let invoke_depth = invoke_context.invoke_depth();
@ -286,7 +324,7 @@ impl Executor for BPFExecutor {
stable_log::program_invoke(&logger, program.unsigned_key(), invoke_depth); stable_log::program_invoke(&logger, program.unsigned_key(), invoke_depth);
let mut instruction_meter = ThisInstructionMeter::new(compute_meter.clone()); let mut instruction_meter = ThisInstructionMeter::new(compute_meter.clone());
let before = compute_meter.borrow().get_remaining(); let before = compute_meter.borrow().get_remaining();
let result = if IS_JIT_ENABLED { let result = if use_jit {
vm.execute_program_jit(&mut instruction_meter) vm.execute_program_jit(&mut instruction_meter)
} else { } else {
vm.execute_program_interpreted(&mut instruction_meter) vm.execute_program_interpreted(&mut instruction_meter)

View File

@ -0,0 +1,5 @@
solana_sdk::declare_builtin!(
solana_sdk::bpf_loader::ID,
solana_bpf_loader_program_with_jit,
solana_bpf_loader_program::process_instruction_jit
);

View File

@ -10085,6 +10085,7 @@ pub(crate) mod tests {
_keyed_accounts: &[KeyedAccount], _keyed_accounts: &[KeyedAccount],
_instruction_data: &[u8], _instruction_data: &[u8],
_invoke_context: &mut dyn InvokeContext, _invoke_context: &mut dyn InvokeContext,
_use_jit: bool,
) -> std::result::Result<(), InstructionError> { ) -> std::result::Result<(), InstructionError> {
Ok(()) Ok(())
} }

View File

@ -241,6 +241,7 @@ pub trait Executor: Debug + Send + Sync {
keyed_accounts: &[KeyedAccount], keyed_accounts: &[KeyedAccount],
instruction_data: &[u8], instruction_data: &[u8],
invoke_context: &mut dyn InvokeContext, invoke_context: &mut dyn InvokeContext,
use_jit: bool,
) -> Result<(), InstructionError>; ) -> Result<(), InstructionError>;
} }

View File

@ -1398,6 +1398,12 @@ pub fn main() {
"Mode to recovery the ledger db write ahead log." "Mode to recovery the ledger db write ahead log."
), ),
) )
.arg(
Arg::with_name("bpf_jit")
.long("bpf-jit")
.takes_value(false)
.help("Use the just-in-time compiler instead of the interpreter for BPF."),
)
.get_matches(); .get_matches();
let identity_keypair = Arc::new(keypair_of(&matches, "identity").unwrap_or_else(Keypair::new)); let identity_keypair = Arc::new(keypair_of(&matches, "identity").unwrap_or_else(Keypair::new));
@ -1540,6 +1546,7 @@ pub fn main() {
poh_verify: !matches.is_present("skip_poh_verify"), poh_verify: !matches.is_present("skip_poh_verify"),
debug_keys, debug_keys,
contact_debug_interval, contact_debug_interval,
bpf_jit: matches.is_present("bpf_jit"),
..ValidatorConfig::default() ..ValidatorConfig::default()
}; };