forked from qmonnet/rbpf
-
Notifications
You must be signed in to change notification settings - Fork 175
/
vm_execution.rs
140 lines (132 loc) · 3.97 KB
/
vm_execution.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright 2020 Solana Maintainers <[email protected]>
//
// Licensed under the Apache License, Version 2.0 <http://www.apache.org/licenses/LICENSE-2.0> or
// the MIT license <http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.
#![feature(test)]
extern crate solana_rbpf;
extern crate test;
use solana_rbpf::{
user_error::UserError,
vm::{Config, DefaultInstructionMeter, EbpfVm, Executable, SyscallRegistry},
};
use std::{fs::File, io::Read};
use test::Bencher;
#[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 = <dyn Executable<UserError, DefaultInstructionMeter>>::from_elf(
&elf,
None,
Config::default(),
SyscallRegistry::default(),
)
.unwrap();
let mut vm =
EbpfVm::<UserError, DefaultInstructionMeter>::new(executable.as_ref(), &mut [], &[])
.unwrap();
bencher.iter(|| {
vm.execute_program_interpreted(&mut DefaultInstructionMeter {})
.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 mut executable = <dyn Executable<UserError, DefaultInstructionMeter>>::from_elf(
&elf,
None,
Config::default(),
SyscallRegistry::default(),
)
.unwrap();
executable.jit_compile().unwrap();
let mut vm =
EbpfVm::<UserError, DefaultInstructionMeter>::new(executable.as_ref(), &mut [], &[])
.unwrap();
bencher.iter(|| {
vm.execute_program_jit(&mut DefaultInstructionMeter {})
.unwrap()
});
}
#[cfg(not(windows))]
fn bench_jit_vs_interpreter(
bencher: &mut Bencher,
assembly: &str,
instruction_meter: u64,
mem: &mut [u8],
) {
let mut executable =
solana_rbpf::assembler::assemble::<UserError, test_utils::TestInstructionMeter>(
assembly,
None,
Config::default(),
SyscallRegistry::default(),
)
.unwrap();
executable.jit_compile().unwrap();
let mut vm = EbpfVm::new(executable.as_ref(), mem, &[]).unwrap();
let interpreter_summary = bencher
.bench(|bencher| {
bencher.iter(|| {
let result =
vm.execute_program_interpreted(&mut test_utils::TestInstructionMeter {
remaining: instruction_meter,
});
assert!(result.is_ok());
assert_eq!(vm.get_total_instruction_count(), instruction_meter);
});
})
.unwrap();
let jit_summary = bencher
.bench(|bencher| {
bencher.iter(|| {
let result = vm.execute_program_jit(&mut test_utils::TestInstructionMeter {
remaining: instruction_meter,
});
assert!(result.is_ok());
assert_eq!(vm.get_total_instruction_count(), instruction_meter);
});
})
.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,
"
mov r1, r2
and r1, 1023
ldindb r1, 0
add r2, 1
jlt r2, 0x10000, -5
exit",
327681,
&mut [0; 1024],
);
}
#[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",
262145,
&mut [0; 0],
);
}