Report compute budget usage (#12931)

This commit is contained in:
Jack May 2020-10-15 15:55:37 -07:00 committed by GitHub
parent 48283161c3
commit b510474dcb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 17 additions and 3 deletions

View File

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