Skip to content

Commit 0f06418

Browse files
committed
Use FIOFFS instead of fsync (on supported platforms)
1 parent de8ba75 commit 0f06418

File tree

7 files changed

+282
-104
lines changed

7 files changed

+282
-104
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ itertools = "0.12.1"
6161
libc = "0.2"
6262
mime_guess = "2.0.4"
6363
nbd = "0.2.3"
64-
nix = { version = "0.28", features = [ "feature", "uio" ] }
64+
nix = { version = "0.28", features = [ "feature", "uio", "ioctl" ] }
6565
num_enum = "0.7"
6666
num-derive = "0.4"
6767
num-traits = "0.2"

downstairs/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,4 @@ asm = ["usdt/asm"]
7171
default = []
7272
zfs_snapshot = []
7373
integration-tests = [] # Enables creating SQLite volumes
74+
omicron-build = [] # Uses FIOFSS for flushes instead of fsync

downstairs/src/extent.rs

+72-6
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,45 @@ pub(crate) trait ExtentInner: Send + Sync + Debug {
3333
fn flush_number(&self) -> Result<u64, CrucibleError>;
3434
fn dirty(&self) -> Result<bool, CrucibleError>;
3535

36-
fn flush(
36+
/// Performs any metadata updates needed before a flush
37+
fn pre_flush(
38+
&mut self,
39+
new_flush: u64,
40+
new_gen: u64,
41+
job_id: JobOrReconciliationId,
42+
) -> Result<(), CrucibleError>;
43+
44+
/// Syncs all relevant data to persistant storage
45+
fn flush_inner(
46+
&mut self,
47+
job_id: JobOrReconciliationId,
48+
) -> Result<(), CrucibleError>;
49+
50+
/// Performs any metadata updates after syncing data to persistent storage
51+
fn post_flush(
3752
&mut self,
3853
new_flush: u64,
3954
new_gen: u64,
4055
job_id: JobOrReconciliationId,
4156
) -> Result<(), CrucibleError>;
4257

58+
/// Performs a full flush (pre/inner/post)
59+
///
60+
/// This is only exposed for the sake of unit testing; normal code should
61+
/// use the fine-grained functions and be forced to consider performance.
62+
#[cfg(test)]
63+
fn flush(
64+
&mut self,
65+
new_flush: u64,
66+
new_gen: u64,
67+
job_id: JobOrReconciliationId,
68+
) -> Result<(), CrucibleError> {
69+
self.pre_flush(new_flush, new_gen, job_id)?;
70+
self.flush_inner(job_id)?;
71+
self.post_flush(new_flush, new_gen, job_id)?;
72+
Ok(())
73+
}
74+
4375
fn read(
4476
&mut self,
4577
job_id: JobId,
@@ -573,22 +605,25 @@ impl Extent {
573605
Ok(())
574606
}
575607

576-
#[instrument]
577-
pub(crate) fn flush<I: Into<JobOrReconciliationId> + Debug>(
608+
/// Prepares for a flush
609+
///
610+
/// Returns `false` if we should skip the flush (because this extent is not
611+
/// dirty), or `true` if we should proceed.
612+
pub(crate) fn pre_flush<I: Into<JobOrReconciliationId> + Debug>(
578613
&mut self,
579614
new_flush: u64,
580615
new_gen: u64,
581616
id: I, // only used for logging
582617
log: &Logger,
583-
) -> Result<(), CrucibleError> {
618+
) -> Result<bool, CrucibleError> {
584619
let job_id: JobOrReconciliationId = id.into();
585620

586621
if !self.inner.dirty()? {
587622
/*
588623
* If we have made no writes to this extent since the last flush,
589624
* we do not need to update the extent on disk
590625
*/
591-
return Ok(());
626+
return Ok(false);
592627
}
593628

594629
// Read only extents should never have the dirty bit set. If they do,
@@ -599,7 +634,38 @@ impl Extent {
599634
crucible_bail!(ModifyingReadOnlyRegion);
600635
}
601636

602-
self.inner.flush(new_flush, new_gen, job_id)
637+
self.inner.pre_flush(new_flush, new_gen, job_id)?;
638+
Ok(true)
639+
}
640+
641+
/// Performs post-flush cleanup
642+
pub(crate) fn post_flush<I: Into<JobOrReconciliationId> + Debug>(
643+
&mut self,
644+
new_flush: u64,
645+
new_gen: u64,
646+
id: I, // only used for logging
647+
) -> Result<(), CrucibleError> {
648+
let job_id: JobOrReconciliationId = id.into();
649+
self.inner.post_flush(new_flush, new_gen, job_id)
650+
}
651+
652+
/// Flushes this extent if it is dirty
653+
#[instrument]
654+
pub(crate) fn flush<
655+
I: Into<JobOrReconciliationId> + Debug + Copy + Clone,
656+
>(
657+
&mut self,
658+
new_flush: u64,
659+
new_gen: u64,
660+
id: I, // only used for logging
661+
log: &Logger,
662+
) -> Result<(), CrucibleError> {
663+
if !self.pre_flush(new_flush, new_gen, id, log)? {
664+
return Ok(());
665+
}
666+
self.inner.flush_inner(id.into())?;
667+
self.post_flush(new_flush, new_gen, id)?;
668+
Ok(())
603669
}
604670

605671
pub fn get_meta_info(&self) -> ExtentMeta {

downstairs/src/extent_inner_raw.rs

+19-14
Original file line numberDiff line numberDiff line change
@@ -378,20 +378,12 @@ impl ExtentInner for RawInner {
378378
Ok(ExtentReadResponse { data: buf, blocks })
379379
}
380380

381-
fn flush(
381+
fn pre_flush(
382382
&mut self,
383383
new_flush: u64,
384384
new_gen: u64,
385385
job_id: JobOrReconciliationId,
386386
) -> Result<(), CrucibleError> {
387-
if !self.dirty()? {
388-
/*
389-
* If we have made no writes to this extent since the last flush,
390-
* we do not need to update the extent on disk
391-
*/
392-
return Ok(());
393-
}
394-
395387
cdt::extent__flush__start!(|| {
396388
(job_id.get(), self.extent_number.0, 0)
397389
});
@@ -400,10 +392,17 @@ impl ExtentInner for RawInner {
400392
// operation atomic.
401393
self.set_flush_number(new_flush, new_gen)?;
402394

395+
Ok(())
396+
}
397+
398+
fn flush_inner(
399+
&mut self,
400+
job_id: JobOrReconciliationId,
401+
) -> Result<(), CrucibleError> {
403402
// Now, we fsync to ensure data is flushed to disk. It's okay to crash
404403
// before this point, because setting the flush number is atomic.
405404
cdt::extent__flush__file__start!(|| {
406-
(job_id.get(), self.extent_number.0, 0)
405+
(job_id.get(), self.extent_number.0)
407406
});
408407
if let Err(e) = self.file.sync_all() {
409408
/*
@@ -416,9 +415,17 @@ impl ExtentInner for RawInner {
416415
}
417416
self.context_slot_dirty.fill(0);
418417
cdt::extent__flush__file__done!(|| {
419-
(job_id.get(), self.extent_number.0, 0)
418+
(job_id.get(), self.extent_number.0)
420419
});
420+
Ok(())
421+
}
421422

423+
fn post_flush(
424+
&mut self,
425+
_new_flush: u64,
426+
_new_gen: u64,
427+
job_id: JobOrReconciliationId,
428+
) -> Result<(), CrucibleError> {
422429
// Check for fragmentation in the context slots leading to worse
423430
// performance, and defragment if that's the case.
424431
let extra_syscalls_per_rw = self
@@ -433,9 +440,7 @@ impl ExtentInner for RawInner {
433440
Ok(())
434441
};
435442

436-
cdt::extent__flush__done!(|| {
437-
(job_id.get(), self.extent_number.0, 0)
438-
});
443+
cdt::extent__flush__done!(|| { (job_id.get(), self.extent_number.0) });
439444

440445
r
441446
}

downstairs/src/extent_inner_sqlite.rs

+46-13
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,32 @@ impl ExtentInner for SqliteInner {
3636
self.0.lock().unwrap().dirty()
3737
}
3838

39-
fn flush(
39+
fn pre_flush(
4040
&mut self,
4141
new_flush: u64,
4242
new_gen: u64,
4343
job_id: JobOrReconciliationId,
4444
) -> Result<(), CrucibleError> {
45-
self.0.lock().unwrap().flush(new_flush, new_gen, job_id)
45+
self.0.lock().unwrap().pre_flush(new_flush, new_gen, job_id)
46+
}
47+
48+
fn flush_inner(
49+
&mut self,
50+
job_id: JobOrReconciliationId,
51+
) -> Result<(), CrucibleError> {
52+
self.0.lock().unwrap().flush_inner(job_id)
53+
}
54+
55+
fn post_flush(
56+
&mut self,
57+
new_flush: u64,
58+
new_gen: u64,
59+
job_id: JobOrReconciliationId,
60+
) -> Result<(), CrucibleError> {
61+
self.0
62+
.lock()
63+
.unwrap()
64+
.post_flush(new_flush, new_gen, job_id)
4665
}
4766

4867
fn read(
@@ -194,10 +213,10 @@ impl SqliteMoreInner {
194213
Ok(self.dirty.get())
195214
}
196215

197-
fn flush(
216+
fn pre_flush(
198217
&mut self,
199-
new_flush: u64,
200-
new_gen: u64,
218+
_new_flush: u64,
219+
_new_gen: u64,
201220
job_id: JobOrReconciliationId,
202221
) -> Result<(), CrucibleError> {
203222
// Used for profiling
@@ -207,12 +226,19 @@ impl SqliteMoreInner {
207226
(job_id.get(), self.extent_number.0, n_dirty_blocks)
208227
});
209228

229+
Ok(())
230+
}
231+
232+
fn flush_inner(
233+
&mut self,
234+
job_id: JobOrReconciliationId,
235+
) -> Result<(), CrucibleError> {
210236
/*
211237
* We must first fsync to get any outstanding data written to disk.
212238
* This must be done before we update the flush number.
213239
*/
214240
cdt::extent__flush__file__start!(|| {
215-
(job_id.get(), self.extent_number.0, n_dirty_blocks)
241+
(job_id.get(), self.extent_number.0)
216242
});
217243
if let Err(e) = self.file.sync_all() {
218244
/*
@@ -225,9 +251,18 @@ impl SqliteMoreInner {
225251
);
226252
}
227253
cdt::extent__flush__file__done!(|| {
228-
(job_id.get(), self.extent_number.0, n_dirty_blocks)
254+
(job_id.get(), self.extent_number.0)
229255
});
230256

257+
Ok(())
258+
}
259+
260+
fn post_flush(
261+
&mut self,
262+
new_flush: u64,
263+
new_gen: u64,
264+
job_id: JobOrReconciliationId,
265+
) -> Result<(), CrucibleError> {
231266
// Clear old block contexts. In order to be crash consistent, only
232267
// perform this after the extent fsync is done. For each block
233268
// written since the last flush, remove all block context rows where
@@ -237,7 +272,7 @@ impl SqliteMoreInner {
237272
// file is rehashed, since in that case we don't have that luxury.
238273

239274
cdt::extent__flush__collect__hashes__start!(|| {
240-
(job_id.get(), self.extent_number.0, n_dirty_blocks)
275+
(job_id.get(), self.extent_number.0)
241276
});
242277

243278
// Rehash any parts of the file that we *may have written* data to since
@@ -250,7 +285,7 @@ impl SqliteMoreInner {
250285
});
251286

252287
cdt::extent__flush__sqlite__insert__start!(|| {
253-
(job_id.get(), self.extent_number.0, n_dirty_blocks)
288+
(job_id.get(), self.extent_number.0)
254289
});
255290

256291
// We put all of our metadb updates into a single transaction to
@@ -265,7 +300,7 @@ impl SqliteMoreInner {
265300
)?;
266301

267302
cdt::extent__flush__sqlite__insert__done!(|| {
268-
(job_id.get(), self.extent_number.0, n_dirty_blocks)
303+
(job_id.get(), self.extent_number.0)
269304
});
270305

271306
self.set_flush_number(new_flush, new_gen)?;
@@ -275,9 +310,7 @@ impl SqliteMoreInner {
275310
// Finally, reset the file's seek offset to 0
276311
self.file.seek(SeekFrom::Start(0))?;
277312

278-
cdt::extent__flush__done!(|| {
279-
(job_id.get(), self.extent_number.0, n_dirty_blocks)
280-
});
313+
cdt::extent__flush__done!(|| { (job_id.get(), self.extent_number.0) });
281314
Ok(())
282315
}
283316

downstairs/src/lib.rs

+8-28
Original file line numberDiff line numberDiff line change
@@ -756,44 +756,24 @@ pub mod cdt {
756756
fn submit__writeunwritten__done(_: u64) {}
757757
fn submit__write__done(_: u64) {}
758758
fn submit__flush__done(_: u64) {}
759-
fn extent__flush__start(job_id: u64, extent_id: u32, extent_size: u64) {}
760-
fn extent__flush__done(job_id: u64, extent_id: u32, extent_size: u64) {}
761-
fn extent__flush__file__start(
759+
fn extent__flush__start(
762760
job_id: u64,
763761
extent_id: u32,
764-
extent_size: u64,
765-
) {
766-
}
767-
fn extent__flush__file__done(
768-
job_id: u64,
769-
extent_id: u32,
770-
extent_size: u64,
771-
) {
772-
}
773-
fn extent__flush__collect__hashes__start(
774-
job_id: u64,
775-
extent_id: u32,
776-
num_dirty: u64,
762+
num_dirty_blocks: u64,
777763
) {
778764
}
765+
fn extent__flush__done(job_id: u64, extent_id: u32) {}
766+
fn extent__flush__file__start(job_id: u64, extent_id: u32) {}
767+
fn extent__flush__file__done(job_id: u64, extent_id: u32) {}
768+
fn extent__flush__collect__hashes__start(job_id: u64, extent_id: u32) {}
779769
fn extent__flush__collect__hashes__done(
780770
job_id: u64,
781771
extent_id: u32,
782772
num_rehashed: u64,
783773
) {
784774
}
785-
fn extent__flush__sqlite__insert__start(
786-
job_id: u64,
787-
extent_id: u32,
788-
extent_size: u64,
789-
) {
790-
}
791-
fn extent__flush__sqlite__insert__done(
792-
_job_id: u64,
793-
_extent_id: u32,
794-
extent_size: u64,
795-
) {
796-
}
775+
fn extent__flush__sqlite__insert__start(job_id: u64, extent_id: u32) {}
776+
fn extent__flush__sqlite__insert__done(job_id: u64, extent_id: u32) {}
797777
fn extent__write__start(job_id: u64, extent_id: u32, n_blocks: u64) {}
798778
fn extent__write__done(job_id: u64, extent_id: u32, n_blocks: u64) {}
799779
fn extent__write__get__hashes__start(

0 commit comments

Comments
 (0)