Skip to content

Commit 81da4e7

Browse files
committed
Assert ds_new cache validity
Assert that the cached job ids in ds_new match the New jobs in ds_active, and that io_state_count is correct. Fixes oxidecomputer#765
1 parent 54942ba commit 81da4e7

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

upstairs/src/lib.rs

+41-1
Original file line numberDiff line numberDiff line change
@@ -3342,15 +3342,55 @@ impl Downstairs {
33423342
* requests for this client.
33433343
*/
33443344
fn new_work(&mut self, client_id: u8) -> Vec<u64> {
3345-
self.ds_new[client_id as usize].drain(..).collect()
3345+
// Compute what should be in the ds_new cache, and assert that the cache
3346+
// is valid. This should only run during debug builds (read: CI) and
3347+
// should not affect release builds as this will degrade performance.
3348+
let computed: usize = self
3349+
.ds_active
3350+
.values()
3351+
.filter_map(|job| {
3352+
if let Some(IOState::New) = job.state.get(&client_id) {
3353+
Some(job.ds_id)
3354+
} else {
3355+
None
3356+
}
3357+
})
3358+
.count();
3359+
3360+
let cached: Vec<u64> =
3361+
self.ds_new[client_id as usize].drain(..).collect();
3362+
3363+
debug_assert_eq!(computed, cached.len());
3364+
3365+
// Assert that the io_state_count accounting is accurate.
3366+
debug_assert_eq!(
3367+
self.io_state_count.new[client_id as usize] as usize,
3368+
cached.len(),
3369+
);
3370+
3371+
cached
33463372
}
33473373

33483374
/**
33493375
* Called to requeue work that was previously found by calling
33503376
* [`new_work`], presumably due to flow control.
33513377
*/
33523378
fn requeue_work(&mut self, client_id: u8, work: &[u64]) {
3379+
// Assert that the work we're requeuing is still new. This should only
3380+
// run during debug builds (read: CI) and should not affect release
3381+
// builds as this will degrade performance.
3382+
for job_id in work {
3383+
let job = self.ds_active.get_mut(job_id).unwrap();
3384+
debug_assert_eq!(*job.state.get(&client_id).unwrap(), IOState::New);
3385+
}
3386+
33533387
self.ds_new[client_id as usize].extend_from_slice(work);
3388+
3389+
// Assert that the io_state_count accounting is accurate.
3390+
debug_assert_eq!(
3391+
self.io_state_count.new[client_id as usize] as usize,
3392+
self.ds_new[client_id as usize].len(),
3393+
);
33543394
}
33553395

33563396
/**

0 commit comments

Comments
 (0)