Skip to content

Commit

Permalink
Merge pull request dragonflyoss#539 from imeoer/jiangliu/fscache-mt
Browse files Browse the repository at this point in the history
nydusd: wrap thread validator as func
  • Loading branch information
jiangliu authored Jun 28, 2022
2 parents 7b0b981 + 2831b4e commit 58c05b4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 37 deletions.
31 changes: 8 additions & 23 deletions src/bin/nydusd/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ use crate::daemon::{DaemonError, NydusDaemon};
use crate::fs_service::{FsBackendMountCmd, FsService};
use crate::service_controller::create_daemon;

use nydus::ensure_threads;

#[cfg(feature = "fusedev")]
mod fusedev;
#[cfg(feature = "virtiofs")]
Expand Down Expand Up @@ -202,6 +204,10 @@ const SHARED_DIR_HELP_MESSAGE: &str = "Directory to share between host and guest
const SHARED_DIR_HELP_MESSAGE: &str =
"Directory to share by FUSE for testing, which also enables `passthroughfs` mode";

pub fn thread_validator(v: String) -> std::result::Result<(), String> {
ensure_threads(v).map(|_| ())
}

#[cfg(any(feature = "fusedev", feature = "virtiofs"))]
fn append_fs_options(app: App<'static, 'static>) -> App<'static, 'static> {
app.arg(
Expand Down Expand Up @@ -270,17 +276,7 @@ fn append_fuse_options(app: App<'static, 'static>) -> App<'static, 'static> {
.help("Number of worker threads to serve IO requests")
.takes_value(true)
.required(false)
.validator(|v| {
if let Ok(t) = v.parse::<i32>() {
if t > 0 && t <= 1024 {
Ok(())
} else {
Err("Invalid working thread number {}, valid values: [1-1024]".to_string())
}
} else {
Err("Input thread number is invalid".to_string())
}
}),
.validator(thread_validator),
)
.arg(
Arg::with_name("writable")
Expand Down Expand Up @@ -351,18 +347,7 @@ fn append_services_subcmd_options(app: App<'static, 'static>) -> App<'static, 's
.help("Number of working threads to serve fscache requests")
.takes_value(true)
.required(false)
.validator(|v| {
if let Ok(t) = v.parse::<i32>() {
if t > 0 && t <= 1024 {
Ok(())
} else {
Err("Invalid working thread number {}, valid values: [1-1024]"
.to_string())
}
} else {
Err("Input thread number is invalid".to_string())
}
}),
.validator(thread_validator),
);

app.subcommand(subcmd)
Expand Down
21 changes: 7 additions & 14 deletions src/bin/nydusd/service_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ use crate::daemon::{
DaemonStateMachineSubscriber,
};
use crate::{FsService, NydusDaemon, SubCmdArgs, DAEMON_CONTROLLER};
#[cfg(target_os = "linux")]
use nydus::ensure_threads;

pub struct ServiceController {
bti: BuildTimeInfo,
Expand Down Expand Up @@ -120,20 +122,11 @@ impl ServiceController {
};
let tag = subargs.value_of("fscache-tag");

let mut threads = 1usize;
if let Some(threads_value) = subargs.value_of("fscache-threads") {
if let Ok(t) = threads_value.parse::<i32>() {
if t > 0 && t <= 1024 {
threads = t as usize;
} else {
return Err(einval!(
"Invalid working thread number {}, valid values: [1-1024]"
));
}
} else {
return Err(einval!("Input thread number is invalid".to_string()));
}
}
let threads = if let Some(threads_value) = subargs.value_of("fscache-threads") {
ensure_threads(threads_value).map_err(|err| einval!(err))?
} else {
1usize
};

info!(
"Create fscache instance at {} with tag {}, {} working threads",
Expand Down
12 changes: 12 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,15 @@ pub struct FsBackendDesc {
pub mounted_time: DateTime<Local>,
pub config: Option<serde_json::Value>,
}

pub fn ensure_threads<V: AsRef<str>>(v: V) -> std::result::Result<usize, String> {
if let Ok(t) = v.as_ref().parse::<usize>() {
if t > 0 && t <= 1024 {
Ok(t)
} else {
Err("Invalid working thread number {}, valid values: [1-1024]".to_string())
}
} else {
Err("Input thread number is invalid".to_string())
}
}

0 comments on commit 58c05b4

Please sign in to comment.