// Copyright 2023 Cartallum Holding Tech // // Licensed under the Apache License, Version 2.0 or // the MIT license , at your option. This file may not be // copied, modified, or distributed except according to those terms. #![feature(test)] extern crate cartes_rbpf; extern crate test; use cartes_rbpf::{ ebpf, elf::Executable, memory_region::MemoryRegion, verifier::{RequisiteVerifier, TautologyVerifier}, vm::{BuiltinProgram, Config, TestContextObject}, }; use std::{fs::File, io::Read, sync::Arc}; use test::Bencher; use test_utils::create_vm; #[bench] fn bench_init_interpreter_execution(bencher: &mut Bencher) { let mut file = File::open("tests/elfs/pass_stack_reference.so").unwrap(); let mut elf = Vec::new(); file.read_to_end(&mut elf).unwrap(); let executable = Executable::::from_elf( &elf, Arc::new(BuiltinProgram::new_loader(Config::default())), ) .unwrap(); let verified_executable = Executable::::verified(executable).unwrap(); let mut context_object = TestContextObject::default(); create_vm!( vm, &verified_executable, &mut context_object, stack, heap, Vec::new(), None ); bencher.iter(|| { vm.context_object_pointer.remaining = 37; vm.execute_program(&verified_executable, true).1.unwrap() }); } #[cfg(not(windows))] #[bench] fn bench_init_jit_execution(bencher: &mut Bencher) { let mut file = File::open("tests/elfs/pass_stack_reference.so").unwrap(); let mut elf = Vec::new(); file.read_to_end(&mut elf).unwrap(); let executable = Executable::::from_elf( &elf, Arc::new(BuiltinProgram::new_loader(Config::default())), ) .unwrap(); let mut verified_executable = Executable::::verified(executable).unwrap(); verified_executable.jit_compile().unwrap(); let mut context_object = TestContextObject::default(); create_vm!( vm, &verified_executable, &mut context_object, stack, heap, Vec::new(), None ); bencher.iter(|| { vm.context_object_pointer.remaining = 37; vm.execute_program(&verified_executable, false).1.unwrap() }); } #[cfg(not(windows))] fn bench_jit_vs_interpreter( bencher: &mut Bencher, assembly: &str, config: Config, instruction_meter: u64, mem: &mut [u8], ) { let executable = cartes_rbpf::assembler::assemble::( assembly, Arc::new(BuiltinProgram::new_loader(config)), ) .unwrap(); let mut verified_executable = Executable::::verified(executable).unwrap(); verified_executable.jit_compile().unwrap(); let mut context_object = TestContextObject::default(); let mem_region = MemoryRegion::new_writable(mem, ebpf::MM_INPUT_START); create_vm!( vm, &verified_executable, &mut context_object, stack, heap, vec![mem_region], None ); let interpreter_summary = bencher .bench(|bencher| { bencher.iter(|| { vm.context_object_pointer.remaining = instruction_meter; let (instruction_count_interpreter, result) = vm.execute_program(&verified_executable, true); assert!(result.is_ok(), "{:?}", result); assert_eq!(instruction_count_interpreter, instruction_meter); }); Ok(()) }) .unwrap() .unwrap(); let jit_summary = bencher .bench(|bencher| { bencher.iter(|| { vm.context_object_pointer.remaining = instruction_meter; let (instruction_count_jit, result) = vm.execute_program(&verified_executable, false); assert!(result.is_ok(), "{:?}", result); assert_eq!(instruction_count_jit, instruction_meter); }); Ok(()) }) .unwrap() .unwrap(); println!( "jit_vs_interpreter_ratio={}", interpreter_summary.mean / jit_summary.mean ); } #[cfg(not(windows))] #[bench] fn bench_jit_vs_interpreter_address_translation(bencher: &mut Bencher) { bench_jit_vs_interpreter( bencher, " ldxb r0, [r1] add r1, 1 mov r0, r1 and r0, 0xFFFFFF jlt r0, 0x20000, -5 exit", Config::default(), 655361, &mut [0; 0x20000], ); } static ADDRESS_TRANSLATION_STACK_CODE: &str = " mov r1, r2 and r1, 4095 mov r3, r10 sub r3, r1 sub r3, 1 ldxb r4, [r3] add r2, 1 jlt r2, 0x10000, -8 exit"; #[cfg(not(windows))] #[bench] fn bench_jit_vs_interpreter_address_translation_stack_fixed(bencher: &mut Bencher) { bench_jit_vs_interpreter( bencher, ADDRESS_TRANSLATION_STACK_CODE, Config { enable_sbpf_v2: false, ..Config::default() }, 524289, &mut [], ); } #[cfg(not(windows))] #[bench] fn bench_jit_vs_interpreter_address_translation_stack_dynamic(bencher: &mut Bencher) { bench_jit_vs_interpreter( bencher, ADDRESS_TRANSLATION_STACK_CODE, Config { enable_sbpf_v2: true, ..Config::default() }, 524289, &mut [], ); } #[cfg(not(windows))] #[bench] fn bench_jit_vs_interpreter_empty_for_loop(bencher: &mut Bencher) { bench_jit_vs_interpreter( bencher, " mov r1, r2 and r1, 1023 add r2, 1 jlt r2, 0x10000, -4 exit", Config::default(), 262145, &mut [0; 0], ); } #[cfg(not(windows))] #[bench] fn bench_jit_vs_interpreter_call_depth_fixed(bencher: &mut Bencher) { bench_jit_vs_interpreter( bencher, " mov r6, 0 add r6, 1 mov r1, 18 call function_foo jlt r6, 1024, -4 exit function_foo: stw [r10-4], 0x11223344 mov r6, r1 jgt r6, 0, +1 exit mov r1, r6 sub r1, 1 call function_foo exit", Config { enable_sbpf_v2: false, ..Config::default() }, 137218, &mut [], ); } #[cfg(not(windows))] #[bench] fn bench_jit_vs_interpreter_call_depth_dynamic(bencher: &mut Bencher) { bench_jit_vs_interpreter( bencher, " mov r6, 0 add r6, 1 mov r1, 18 call function_foo jlt r6, 1024, -4 exit function_foo: sub r11, 4 stw [r10-4], 0x11223344 mov r6, r1 jeq r6, 0, +3 mov r1, r6 sub r1, 1 call function_foo add r11, 4 exit", Config { enable_sbpf_v2: true, ..Config::default() }, 176130, &mut [], ); }