Report compute budget usage (#12931) (#12934)

(cherry picked from commit b510474dcb)

Co-authored-by: Jack May <jack@solana.com>
This commit is contained in:
mergify[bot]
2020-10-16 00:05:18 +00:00
committed by GitHub
parent a443e2e773
commit e7062de05f

View File

@ -194,6 +194,11 @@ pub fn process_instruction(
struct ThisInstructionMeter { struct ThisInstructionMeter {
compute_meter: Rc<RefCell<dyn ComputeMeter>>, compute_meter: Rc<RefCell<dyn ComputeMeter>>,
} }
impl ThisInstructionMeter {
fn new(compute_meter: Rc<RefCell<dyn ComputeMeter>>) -> Self {
Self { compute_meter }
}
}
impl InstructionMeter for ThisInstructionMeter { impl InstructionMeter for ThisInstructionMeter {
fn consume(&mut self, amount: u64) { fn consume(&mut self, amount: u64) {
// 1 to 1 instruction to compute unit mapping // 1 to 1 instruction to compute unit mapping
@ -245,13 +250,22 @@ impl Executor for BPFExecutor {
}; };
log!(logger, "Call BPF program {}", program.unsigned_key()); log!(logger, "Call BPF program {}", program.unsigned_key());
let instruction_meter = ThisInstructionMeter { compute_meter }; let instruction_meter = ThisInstructionMeter::new(compute_meter.clone());
match vm.execute_program_metered( let before = compute_meter.borrow().get_remaining();
let result = vm.execute_program_metered(
parameter_bytes.as_slice(), parameter_bytes.as_slice(),
&[], &[],
&[heap_region], &[heap_region],
instruction_meter, instruction_meter,
) { );
let after = compute_meter.borrow().get_remaining();
log!(
logger,
"BPF program consumed {} of {} units",
before - after,
before
);
match result {
Ok(status) => { Ok(status) => {
if status != SUCCESS { if status != SUCCESS {
let error: InstructionError = status.into(); let error: InstructionError = status.into();