This repository has been archived by the owner on Jan 10, 2025. It is now read-only.
forked from qmonnet/rbpf
-
Notifications
You must be signed in to change notification settings - Fork 180
/
Copy pathmemory_mapping.rs
211 lines (195 loc) · 6.88 KB
/
memory_mapping.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// 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 rand;
extern crate solana_rbpf;
extern crate test;
use rand::{rngs::SmallRng, Rng, SeedableRng};
use solana_rbpf::{
memory_region::{AccessType, MemoryMapping, MemoryRegion},
user_error::UserError,
vm::Config,
};
use test::Bencher;
fn generate_memory_regions(
entries: usize,
is_writable: bool,
mut prng: Option<&mut SmallRng>,
) -> (Vec<MemoryRegion>, u64) {
let mut memory_regions = Vec::with_capacity(entries + 1);
memory_regions.push(MemoryRegion::default());
let mut offset = 0x100000000;
for _ in 0..entries {
let length = match &mut prng {
Some(prng) => (*prng).gen::<u8>() as u64 + 4,
None => 4,
};
let content = vec![0; length as usize];
memory_regions.push(MemoryRegion::new_for_testing(
&content[..],
offset,
0,
is_writable,
));
offset += 0x100000000;
}
(memory_regions, offset)
}
macro_rules! new_prng {
( ) => {
SmallRng::from_seed([0; 32])
};
}
#[bench]
fn bench_prng(bencher: &mut Bencher) {
let mut prng = new_prng!();
bencher.iter(|| prng.gen::<u64>());
}
#[bench]
fn bench_gapped_randomized_access_with_1024_entries(bencher: &mut Bencher) {
let frame_size: u64 = 2;
let frame_count: u64 = 1024;
let content = vec![0; (frame_size * frame_count * 2) as usize];
let memory_regions = vec![
MemoryRegion::default(),
MemoryRegion::new_for_testing(&content[..], 0x100000000, frame_size, false),
];
let config = Config::default();
let memory_mapping = MemoryMapping::new::<UserError>(memory_regions, &config).unwrap();
let mut prng = new_prng!();
bencher.iter(|| {
assert!(memory_mapping
.map::<UserError>(
AccessType::Load,
0x100000000 + (prng.gen::<u64>() % frame_count * (frame_size * 2)),
1
)
.is_ok());
});
}
#[bench]
fn bench_randomized_access_with_0001_entry(bencher: &mut Bencher) {
let content = vec![0; 1024 * 2];
let memory_regions = vec![
MemoryRegion::default(),
MemoryRegion::new_readonly(&content[..], 0x100000000),
];
let config = Config::default();
let memory_mapping = MemoryMapping::new::<UserError>(memory_regions, &config).unwrap();
let mut prng = new_prng!();
bencher.iter(|| {
let _ = memory_mapping.map::<UserError>(
AccessType::Load,
0x100000000 + (prng.gen::<u64>() % content.len() as u64),
1,
);
});
}
#[bench]
fn bench_randomized_mapping_access_with_0004_entries(bencher: &mut Bencher) {
let mut prng = new_prng!();
let (memory_regions, end_address) = generate_memory_regions(4, false, Some(&mut prng));
let config = Config::default();
let memory_mapping = MemoryMapping::new::<UserError>(memory_regions, &config).unwrap();
bencher.iter(|| {
let _ = memory_mapping.map::<UserError>(
AccessType::Load,
0x100000000 + (prng.gen::<u64>() % end_address),
1,
);
});
}
#[bench]
fn bench_randomized_mapping_access_with_0016_entries(bencher: &mut Bencher) {
let mut prng = new_prng!();
let (memory_regions, end_address) = generate_memory_regions(16, false, Some(&mut prng));
let config = Config::default();
let memory_mapping = MemoryMapping::new::<UserError>(memory_regions, &config).unwrap();
bencher.iter(|| {
let _ = memory_mapping.map::<UserError>(
AccessType::Load,
0x100000000 + (prng.gen::<u64>() % end_address),
1,
);
});
}
#[bench]
fn bench_randomized_mapping_access_with_0064_entries(bencher: &mut Bencher) {
let mut prng = new_prng!();
let (memory_regions, end_address) = generate_memory_regions(64, false, Some(&mut prng));
let config = Config::default();
let memory_mapping = MemoryMapping::new::<UserError>(memory_regions, &config).unwrap();
bencher.iter(|| {
let _ = memory_mapping.map::<UserError>(
AccessType::Load,
0x100000000 + (prng.gen::<u64>() % end_address),
1,
);
});
}
#[bench]
fn bench_randomized_mapping_access_with_0256_entries(bencher: &mut Bencher) {
let mut prng = new_prng!();
let (memory_regions, end_address) = generate_memory_regions(256, false, Some(&mut prng));
let config = Config::default();
let memory_mapping = MemoryMapping::new::<UserError>(memory_regions, &config).unwrap();
bencher.iter(|| {
let _ = memory_mapping.map::<UserError>(
AccessType::Load,
0x100000000 + (prng.gen::<u64>() % end_address),
1,
);
});
}
#[bench]
fn bench_randomized_mapping_access_with_1024_entries(bencher: &mut Bencher) {
let mut prng = new_prng!();
let (memory_regions, end_address) = generate_memory_regions(1024, false, Some(&mut prng));
let config = Config::default();
let memory_mapping = MemoryMapping::new::<UserError>(memory_regions, &config).unwrap();
bencher.iter(|| {
let _ = memory_mapping.map::<UserError>(
AccessType::Load,
0x100000000 + (prng.gen::<u64>() % end_address),
1,
);
});
}
#[bench]
fn bench_randomized_access_with_1024_entries(bencher: &mut Bencher) {
let mut prng = new_prng!();
let (memory_regions, end_address) = generate_memory_regions(1024, false, None);
let config = Config::default();
let memory_mapping = MemoryMapping::new::<UserError>(memory_regions, &config).unwrap();
bencher.iter(|| {
let _ = memory_mapping.map::<UserError>(
AccessType::Load,
0x100000000 + (prng.gen::<u64>() % end_address),
1,
);
});
}
#[bench]
fn bench_randomized_mapping_with_1024_entries(bencher: &mut Bencher) {
let mut prng = new_prng!();
let (memory_regions, _end_address) = generate_memory_regions(1024, false, Some(&mut prng));
let config = Config::default();
let memory_mapping = MemoryMapping::new::<UserError>(memory_regions, &config).unwrap();
bencher.iter(|| {
let _ = memory_mapping.map::<UserError>(AccessType::Load, 0x100000000, 1);
});
}
#[bench]
fn bench_mapping_with_1024_entries(bencher: &mut Bencher) {
let (memory_regions, _end_address) = generate_memory_regions(1024, false, None);
let config = Config::default();
let memory_mapping = MemoryMapping::new::<UserError>(memory_regions, &config).unwrap();
bencher.iter(|| {
assert!(memory_mapping
.map::<UserError>(AccessType::Load, 0x100000000, 1)
.is_ok());
});
}