From 3a7199c09819bbbd558f2696b7353acdbc694999 Mon Sep 17 00:00:00 2001 From: Liang Li Date: Thu, 22 Jul 2021 02:44:14 +0000 Subject: [PATCH 1/3] dysche-cli: add 'show' subcommand Intend to show detail of a partition. Signed-off-by: Liang Li --- dysche-cli/src/main.rs | 10 ++++++++++ dysche-cli/src/param.yml | 11 ++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/dysche-cli/src/main.rs b/dysche-cli/src/main.rs index f5c032a..77bd1ec 100644 --- a/dysche-cli/src/main.rs +++ b/dysche-cli/src/main.rs @@ -45,6 +45,9 @@ fn main() { } else if let Some(sc) = matches.subcommand_matches("destroy") { let pid = sc.value_of("pid").unwrap_or("-1"); ret = destroy_partition(pid); + } else if let Some(sc) = matches.subcommand_matches("show") { + let pid = sc.value_of("pid").unwrap_or("-1"); + ret = show_partition(pid); } else if let Some(sc) = matches.subcommand_matches("migrate") { println!("migrate resources between partitions:"); @@ -136,6 +139,13 @@ fn destroy_partition(pid: &str) -> i32 { return _ret; } +fn show_partition(pid: &str) -> i32 { + println!("The details of the partition {}", pid); + println!("place holder. need impl later."); + + return 0; +} + fn migrate_partition(_sp: &str, _dp: &str, _cpus: &str) -> i32 { println!("place holder. need impl later."); diff --git a/dysche-cli/src/param.yml b/dysche-cli/src/param.yml index 650cb94..b692395 100644 --- a/dysche-cli/src/param.yml +++ b/dysche-cli/src/param.yml @@ -61,4 +61,13 @@ subcommands: args: - verbose: help: "show partition info verbosely" - short: v \ No newline at end of file + short: v + + - show: + about: "show details of a partition" + multiple: false + args: + - pid: + help: "specify partition id that details need be shown." + takes_value: true + short: p -- Gitee From e698da4da93603120842772c974851e6fa5cd8b2 Mon Sep 17 00:00:00 2001 From: "Liang Li (Euler)" Date: Fri, 23 Jul 2021 10:54:13 +0800 Subject: [PATCH 2/3] add 'nix' crate into dependency Prepare for *nix functions. Signed-off-by: Liang Li (Euler) --- dysche-cli/Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/dysche-cli/Cargo.toml b/dysche-cli/Cargo.toml index d3e541f..d4c1cdd 100644 --- a/dysche-cli/Cargo.toml +++ b/dysche-cli/Cargo.toml @@ -7,3 +7,4 @@ edition = "2018" [dependencies] clap = { version = "~2.27.0", features = ["suggestions", "color", "yaml"] } +nix = "0.22.0" -- Gitee From 19d9e0440aa25623f8c4e59c1feff1da30a6fcf2 Mon Sep 17 00:00:00 2001 From: "Liang Li (Euler)" Date: Fri, 23 Jul 2021 10:54:39 +0800 Subject: [PATCH 3/3] dysche-cli: switch to Result as much as possible Seems like Result is kind of more rust-natural than i32 as return value of functions. Signed-off-by: Liang Li (Euler) --- dysche-cli/src/main.rs | 59 +++++++++++++++++++++++++++--------------- 1 file changed, 38 insertions(+), 21 deletions(-) diff --git a/dysche-cli/src/main.rs b/dysche-cli/src/main.rs index 77bd1ec..1ed2137 100644 --- a/dysche-cli/src/main.rs +++ b/dysche-cli/src/main.rs @@ -10,7 +10,7 @@ const DYSCHE_STS : &str = "/sys/modules/dysche/status"; fn main() { let yml = clap::load_yaml!("param.yml"); let matches = clap::App::from_yaml(yml).get_matches(); - let mut ret = 0; + let mut ret : Result = Ok(0); if let Some(_sc) = matches.subcommand_matches("list") { let mut verb = false; @@ -28,19 +28,22 @@ fn main() { let kernel = sc.value_of("kernel").unwrap_or(""); if cpus == "" { - ret = 1; + ret = Err(1); println!("Core (lists) is needed."); } if kernel == "" { - ret = 1; + ret = Err(2); println!("kernel for the newly created partition is needed."); } - if ret <= 0 { - println!(" creating a new partition, running {} on cores {}", kernel, + match ret { + Ok(_) => { + println!(" creating a new partition, running {} on cores {}", kernel, cpus); - ret = create_partition(cpus, kernel, "console=ttyS0, 115200", "acpi_devs: "); + let _ret = create_partition(cpus, kernel, "console=ttyS0, 115200", "acpi_devs: "); + }, + Err(_) => {}, } } else if let Some(sc) = matches.subcommand_matches("destroy") { let pid = sc.value_of("pid").unwrap_or("-1"); @@ -56,24 +59,35 @@ fn main() { let dp = sc.value_of("dest_partition").unwrap_or(""); if cpus == "" { - ret = 1; + ret = Err(3); println!("Core (lists) is needed."); } if sp == "" || dp == "" { - ret = 1; + ret = Err(4); println!("source & dest partitions need be specified."); } - if ret <= 0 { - println!(" migrate cpu {} from partition {} to partition {}", cpus, + match ret { + Ok(_) => { + println!(" migrate cpu {} from partition {} to partition {}", cpus, sp, dp); - ret = migrate_partition(sp, dp, cpus); + ret = migrate_partition(sp, dp, cpus); + }, + Err(_) => {}, } } - if ret > 0 { - let _ = clap::App::from_yaml(yml).print_long_help(); + match ret { + Ok(_) => println!("success."), + Err(e) => { + println!("errcode {}", e); + match e { + 1 => {}, + _ => {}, + } + let _ = clap::App::from_yaml(yml).print_long_help(); + }, } } @@ -98,7 +112,7 @@ fn list_partitions(_verbose: bool) -> i32 { return ret; } -fn create_partition(cpus: &str, kernel_img: &str, kernel_param: &str, dev_list: &str) -> i32 { +fn create_partition(cpus: &str, kernel_img: &str, kernel_param: &str, dev_list: &str) -> Result { let mut ret = 0; println!("Info: cpu: {}, kernel: {}, kernel_param: {}, dev_list: {}", cpus, kernel_img, kernel_param, dev_list); @@ -116,10 +130,13 @@ fn create_partition(cpus: &str, kernel_img: &str, kernel_param: &str, dev_list: ret = 1; } - return ret; + match ret { + 0 => Ok(ret), + _ => Err(ret), + } } -fn destroy_partition(pid: &str) -> i32 { +fn destroy_partition(pid: &str) -> Result { let mut _ret = 0; println!("Will force destroy partition {}", pid); @@ -136,20 +153,20 @@ fn destroy_partition(pid: &str) -> i32 { _ret = 1; } - return _ret; + return Ok(_ret); } -fn show_partition(pid: &str) -> i32 { +fn show_partition(pid: &str) -> Result { println!("The details of the partition {}", pid); println!("place holder. need impl later."); - return 0; + return Ok(0); } -fn migrate_partition(_sp: &str, _dp: &str, _cpus: &str) -> i32 { +fn migrate_partition(_sp: &str, _dp: &str, _cpus: &str) -> Result { println!("place holder. need impl later."); - return 0; + return Ok(0); } fn write_line(filename: &str, line: &str) -> i32 { -- Gitee