From 4e4b7ca2b12ba472bc2747487f5ea0c9773936d0 Mon Sep 17 00:00:00 2001 From: Joe Thornber Date: Mon, 10 Aug 2020 11:24:50 +0100 Subject: [PATCH] [thin_check (rust)] add --sync-io flag Makes it easier to switch between engines --- src/bin/thin_check.rs | 15 ++++++++++++++- src/thin/check.rs | 21 +++++++++++++++++---- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/src/bin/thin_check.rs b/src/bin/thin_check.rs index 46ae7ef..2c74c46 100644 --- a/src/bin/thin_check.rs +++ b/src/bin/thin_check.rs @@ -5,6 +5,7 @@ use clap::{App, Arg}; use std::path::Path; use std::process; use thinp::file_utils; +use thinp::thin::check::{check, ThinCheckOptions}; use std::process::exit; @@ -56,6 +57,13 @@ fn main() { .help("Specify the input device to check") .required(true) .index(1), + ) + .arg( + Arg::with_name("SYNC_IO") + .help("Force use of synchronous io") + .long("sync-io") + .value_name("SYNC_IO") + .takes_value(false), ); let matches = parser.get_matches(); @@ -66,7 +74,12 @@ fn main() { exit(1); } - if let Err(reason) = thinp::thin::check::check(&input_file) { + let opts = ThinCheckOptions { + dev: &input_file, + async_io: !matches.is_present("SYNC_IO"), + }; + + if let Err(reason) = check(&opts) { println!("Application error: {}", reason); process::exit(1); } diff --git a/src/thin/check.rs b/src/thin/check.rs index 508a206..fb73321 100644 --- a/src/thin/check.rs +++ b/src/thin/check.rs @@ -264,10 +264,23 @@ impl<'a> NodeVisitor for OverflowChecker<'a> { const MAX_CONCURRENT_IO: u32 = 1024; -pub fn check(dev: &Path) -> Result<()> { - let nr_threads = 4; - let engine = Arc::new(AsyncIoEngine::new(dev, MAX_CONCURRENT_IO)?); - //let engine: Arc = Arc::new(SyncIoEngine::new(dev, nr_threads)?); +pub struct ThinCheckOptions<'a> { + pub dev: &'a Path, + pub async_io: bool, +} + +pub fn check(opts: &ThinCheckOptions) -> Result<()> { + let engine: Arc; + + let nr_threads; + if opts.async_io { + nr_threads = std::cmp::min(4, num_cpus::get()); + engine = Arc::new(AsyncIoEngine::new(opts.dev, MAX_CONCURRENT_IO)?); + } else { + eprintln!("falling back to synchronous io"); + nr_threads = num_cpus::get() * 2; + engine = Arc::new(SyncIoEngine::new(opts.dev, nr_threads)?); + } let now = Instant::now(); let sb = read_superblock(engine.as_ref(), SUPERBLOCK_LOCATION)?;