/
bufio.rs
356 lines (291 loc) · 8.86 KB
/
bufio.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
use crate::emulator::memory::*;
use crate::fixture::*;
use crate::guest::*;
use crate::stubs::*;
use crate::test_runner::*;
use crate::wrappers::bufio::*;
use anyhow::{anyhow, Result};
use std::collections::BTreeSet;
//-------------------------------
fn auto_lru(fix: &mut Fixture) -> Result<(AutoGPtr<'_>, Addr)> {
let lru = Lru {
cursor: Addr(0),
count: 0,
};
let ptr = alloc_guest(&mut fix.vm.mem, &lru, PERM_READ | PERM_WRITE)?;
Ok((AutoGPtr::new(fix, ptr), ptr))
}
// The lru entry is the first field of buffers. Let's put
// this assumption in a single place.
fn entry_to_buf(e: Addr) -> Addr {
e
}
// Ditto
fn list_to_entry(l: Addr) -> Addr {
l
}
fn lru_read_buffers(fix: &Fixture, lru: Addr) -> Result<Vec<Buffer>> {
let mut bufs = Vec::new();
let lru = read_guest::<Lru>(&fix.vm.mem, lru)?;
let mut cursor = lru.cursor;
if cursor.0 != 0 {
let first = cursor;
loop {
let buf_addr = entry_to_buf(list_to_entry(cursor));
let buf = read_guest::<Buffer>(&fix.vm.mem, buf_addr)?;
cursor = buf.lru.list.next;
bufs.push(buf);
if cursor == first {
break;
}
}
}
Ok(bufs)
}
fn test_lru_create(fix: &mut Fixture) -> Result<()> {
standard_globals(fix)?;
let (mut fix, lru) = auto_lru(fix)?;
lru_exit(&mut fix, lru)?;
Ok(())
}
fn test_lru_inserts(fix: &mut Fixture, blocks: &[u64]) -> Result<()> {
standard_globals(fix)?;
let (mut fix, lru) = auto_lru(fix)?;
let bufs: Vec<Buffer> = blocks
.iter()
.map({
|b| Buffer {
lru: LruEntry::default(),
block: *b,
hold_count: 0,
last_accessed: 0,
list_mode: LruKind::Clean,
}
})
.collect();
let (mut fix, guest_bufs) = auto_alloc_vec(&mut fix, &bufs)?;
for b in &guest_bufs {
lru_insert(&mut fix, lru, *b)?;
}
let count = lru_count(&fix, lru)?;
if count != blocks.len() as u64 {
return Err(anyhow!(
"lru counts not correct, actual {}, expected {}",
count,
blocks.len()
));
}
let bufs = lru_read_buffers(&fix, lru)?;
if bufs.len() != blocks.len() {
return Err(anyhow!("too few buffers"));
}
for (i, b) in blocks.iter().enumerate() {
if bufs[i].block != *b {
return Err(anyhow!("incorrect block nr"));
}
}
Ok(())
}
fn test_lru_insert_1(fix: &mut Fixture) -> Result<()> {
test_lru_inserts(fix, &[1234])
}
fn test_lru_insert_many(fix: &mut Fixture) -> Result<()> {
let blocks: Vec<u64> = (0..1024).collect();
test_lru_inserts(fix, &blocks)
}
fn seq_buffers(b: usize, e: usize) -> Vec<Buffer> {
(b..e)
.map({
|b| Buffer {
lru: LruEntry::default(),
block: b as u64,
hold_count: 0,
last_accessed: 0,
list_mode: LruKind::Clean,
}
})
.collect()
}
fn test_lru_evict(fix: &mut Fixture) -> Result<()> {
standard_globals(fix)?;
let (mut fix, lru) = auto_lru(fix)?;
let bufs = seq_buffers(0, 1024);
let (mut fix, guest_bufs) = auto_alloc_vec(&mut fix, &bufs)?;
for b in &guest_bufs {
lru_insert(&mut fix, lru, *b)?;
}
// reference every other block
for (i, b) in guest_bufs.iter().enumerate() {
if i % 2 == 0 {
lru_reference(&mut fix, *b)?;
}
}
let pred = fix.const_callback(0)?; // ER_EVICT
// evict half the buffers, and check they aren't the
// ones that were referenced.
for _ in 0..(bufs.len() / 2) {
let b_ptr = lru_evict(&mut fix, lru, pred, Addr(0))?;
if b_ptr == Addr(0) {
return Err(anyhow!("evict failed"));
}
let buf = read_guest::<Buffer>(&fix.vm.mem, b_ptr)?;
if buf.block % 2 != 1 {
return Err(anyhow!(
"unexpected block chosen for eviction {}",
buf.block
));
}
}
// Insert another batch
let bufs2 = seq_buffers(1024, 2048);
let (mut fix, guest_bufs2) = auto_alloc_vec(&mut fix, &bufs2)?;
for b in &guest_bufs2 {
lru_insert(&mut fix, lru, *b)?;
}
// Reference the older buffers
for (i, b) in guest_bufs.iter().enumerate() {
if i % 2 == 0 {
lru_reference(&mut fix, *b)?;
}
}
// Evict the second batch
for _ in 0..bufs2.len() {
let b_ptr = lru_evict(&mut fix, lru, pred, Addr(0))?;
if b_ptr == Addr(0) {
return Err(anyhow!("evict failed"));
}
let buf = read_guest::<Buffer>(&fix.vm.mem, b_ptr)?;
if buf.block < 1024 {
return Err(anyhow!(
"unexpected block chosen for eviction {}",
buf.block
));
}
}
fix.trace_func("lru_insert")?;
fix.trace_func("lru_evict")?;
// Reinsert the second batch
for b in &guest_bufs2 {
lru_insert(&mut fix, lru, *b)?;
}
// evict the remains of the original batch.
for _ in 0..(bufs.len() / 2) {
let b_ptr = lru_evict(&mut fix, lru, pred, Addr(0))?;
if b_ptr == Addr(0) {
return Err(anyhow!("evict failed"));
}
let buf = read_guest::<Buffer>(&fix.vm.mem, b_ptr)?;
if buf.block >= 1024 {
return Err(anyhow!(
"unexpected block chosen for eviction {}",
buf.block
));
}
}
Ok(())
}
//-------------------------------
fn auto_cache(fix: &mut Fixture) -> Result<(AutoGPtr<'_>, Addr)> {
auto_alloc(fix, BUFFER_CACHE_SIZE)
}
fn test_cache_create(fix: &mut Fixture) -> Result<()> {
standard_globals(fix)?;
let (mut fix, cache) = auto_cache(fix)?;
cache_exit(&mut fix, cache)?;
Ok(())
}
fn test_cache_insert(fix: &mut Fixture) -> Result<()> {
standard_globals(fix)?;
let (mut fix, cache) = auto_cache(fix)?;
// Insert a bunch of buffers
let bufs = seq_buffers(0, 1024);
let (mut fix, guest_bufs) = auto_alloc_vec(&mut fix, &bufs)?;
for b in &guest_bufs {
cache_insert(&mut fix, cache, *b)?;
}
// Remove them all
for (i, b) in bufs.iter().enumerate() {
let b2 = cache_get(&mut fix, cache, b.block)?;
if guest_bufs[i] != b2 {
return Err(anyhow!("block mismatch"));
}
cache_remove(&mut fix, cache, b2)?;
}
cache_exit(&mut fix, cache)?;
Ok(())
}
fn test_cache_mark(fix: &mut Fixture) -> Result<()> {
standard_globals(fix)?;
let (mut fix, cache) = auto_cache(fix)?;
// Insert a bunch of buffers
let bufs = seq_buffers(0, 1024);
let (mut fix, guest_bufs) = auto_alloc_vec(&mut fix, &bufs)?;
for b in &guest_bufs {
cache_insert(&mut fix, cache, *b)?;
}
// Mark half as dirty
let mut dirty: BTreeSet<Addr> = BTreeSet::new();
for b in &bufs {
if b.block % 2 == 0 {
continue;
}
let b2 = cache_get(&mut fix, cache, b.block)?;
cache_mark(&mut fix, cache, b2, LruKind::Dirty)?;
cache_put(&mut fix, cache, b2)?;
dirty.insert(b2);
}
// check they are indeed dirty
let mut dirty2 = BTreeSet::new();
loop {
let pred = fix.const_callback(0)?; // ER_EVICT
let b = cache_evict(&mut fix, cache, LruKind::Dirty, pred, Addr(0))?;
if b.0 == 0 {
break;
}
dirty2.insert(b);
}
if dirty != dirty2 {
return Err(anyhow!("missing dirty buffer"));
}
// Remove them all, this time using remove_range
let pred = fix.const_callback(0)?; // ER_EVICT
let release = fix.const_callback(0)?;
cache_remove_range(&mut fix, cache, 0, 1024, pred, release)?;
cache_exit(&mut fix, cache)?;
Ok(())
}
//-------------------------------
pub fn register_tests(tests: &mut TestSet) -> Result<()> {
let kmodules = vec![BUFIO_MOD];
let mut prefix: Vec<&'static str> = Vec::new();
macro_rules! test_section {
($path:expr, $($s:stmt)*) => {{
prefix.push($path);
$($s)*
prefix.pop().unwrap();
}}
}
macro_rules! test {
($path:expr, $func:expr) => {{
prefix.push($path);
let p = prefix.concat();
prefix.pop().unwrap();
tests.register(&p, Test::new(kmodules.clone(), Box::new($func)));
}};
}
test_section! {
"/bufio/lru/",
test!("create", test_lru_create)
test!("insert-1", test_lru_insert_1)
test!("insert-many", test_lru_insert_many)
test!("evict", test_lru_evict)
};
test_section! {
"/bufio/cache/",
test!("create", test_cache_create)
test!("insert", test_cache_insert)
test!("mark", test_cache_mark)
};
Ok(())
}
//-------------------------------