Skip to content

Commit

Permalink
Merge pull request #211 from NordicHPC/radovan/cosmetics
Browse files Browse the repository at this point in the history
Auto-formatting and clippy-simplifications
  • Loading branch information
lars-t-hansen authored Nov 20, 2024
2 parents 31b2486 + ee358b4 commit 34e0cbb
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 65 deletions.
42 changes: 18 additions & 24 deletions src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,30 +118,24 @@ pub fn safe_command(
Ok(stdout_result)
}
}
Ok(ExitStatus::Exited(126)) => {
Err(CmdError::CouldNotStart(format_failure(
command,
"Command cannot execute",
&stdout_result,
&stderr_result,
)))
}
Ok(ExitStatus::Exited(127)) => {
Err(CmdError::CouldNotStart(format_failure(
command,
"Command not found",
&stdout_result,
&stderr_result,
)))
}
Ok(ExitStatus::Signaled(15)) => {
Err(CmdError::Hung(format_failure(
command,
"Killed by SIGTERM",
&stdout_result,
&stderr_result,
)))
}
Ok(ExitStatus::Exited(126)) => Err(CmdError::CouldNotStart(format_failure(
command,
"Command cannot execute",
&stdout_result,
&stderr_result,
))),
Ok(ExitStatus::Exited(127)) => Err(CmdError::CouldNotStart(format_failure(
command,
"Command not found",
&stdout_result,
&stderr_result,
))),
Ok(ExitStatus::Signaled(15)) => Err(CmdError::Hung(format_failure(
command,
"Killed by SIGTERM",
&stdout_result,
&stderr_result,
))),
Ok(x) => Err(CmdError::Failed(format_failure(
command,
format!("Unspecified other exit status {:?}", x).as_str(),
Expand Down
12 changes: 5 additions & 7 deletions src/hostname.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,11 @@ use std::os::unix::ffi::OsStringExt;

pub fn get() -> String {
match primitive_get() {
Ok(hn) => {
match hn.into_string() {
Ok(s) => s,
Err(_) => "unknown-host".to_string()
}
}
Err(_) => "unknown-host".to_string()
Ok(hn) => match hn.into_string() {
Ok(s) => s,
Err(_) => "unknown-host".to_string(),
},
Err(_) => "unknown-host".to_string(),
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/nvidia.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl gpu::GPU for NvidiaGPU {

fn get_card_configuration(&mut self) -> Result<Vec<gpu::Card>, String> {
if self.info.is_none() {
self.info = Some(get_nvidia_configuration(&vec!["-a"]))
self.info = Some(get_nvidia_configuration(&["-a"]))
}
match self.info.as_ref().unwrap() {
Ok(data) => Ok(data
Expand All @@ -59,7 +59,7 @@ impl gpu::GPU for NvidiaGPU {

fn get_card_utilization(&mut self) -> Result<Vec<gpu::CardState>, String> {
if self.info.is_none() {
self.info = Some(get_nvidia_configuration(&vec!["-a"]))
self.info = Some(get_nvidia_configuration(&["-a"]))
}
match self.info.as_ref().unwrap() {
Ok(data) => Ok(data
Expand Down Expand Up @@ -795,7 +795,7 @@ fn test_parse_nvidia_configuration() {
assert!(c.info.driver == "545.23.08");
assert!(c.info.firmware == "12.3");
assert!(c.info.uuid == "GPU-198d6802-0000-0000-0000-000000000000");
assert!(c.info.mem_size_kib == 11264*1024);
assert!(c.info.mem_size_kib == 11264 * 1024);
assert!(c.info.power_limit_watt == 250);
assert!(c.info.max_power_limit_watt == 280);
assert!(c.info.min_power_limit_watt == 100);
Expand All @@ -806,8 +806,8 @@ fn test_parse_nvidia_configuration() {
assert!(c.state.fan_speed_pct == 28.0);
assert!(c.state.compute_mode == "Default");
assert!(c.state.perf_state == "P8");
assert!(c.state.mem_reserved_kib == 252*1024);
assert!(c.state.mem_used_kib == 3*1024);
assert!(c.state.mem_reserved_kib == 252 * 1024);
assert!(c.state.mem_used_kib == 3 * 1024);
assert!(c.state.gpu_utilization_pct == 5.0);
assert!(c.state.mem_utilization_pct == 8.0);
assert!(c.state.temp_c == 34);
Expand Down
21 changes: 12 additions & 9 deletions src/procfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,10 @@ pub fn get_process_information(
if l.starts_with("cpu ") {
cpu_total_secs = sum / ticks_per_sec;
} else {
let cpu_no =
match fields[0][3..].parse::<usize>() {
Ok(x) => x,
Err(_) => { continue } // Too harsh to error out
};
let cpu_no = match fields[0][3..].parse::<usize>() {
Ok(x) => x,
Err(_) => continue, // Too harsh to error out
};
if per_cpu_secs.len() < cpu_no + 1 {
per_cpu_secs.resize(cpu_no + 1, 0u64);
}
Expand Down Expand Up @@ -207,7 +206,9 @@ pub fn get_process_information(
(Some(commstart), Some(commend)) => {
comm = line[commstart + 1..commend].to_string();
field_storage = line[commend + 1..].trim().to_string();
fields = field_storage.split_ascii_whitespace().collect::<Vec<&str>>();
fields = field_storage
.split_ascii_whitespace()
.collect::<Vec<&str>>();
}
};

Expand Down Expand Up @@ -561,7 +562,8 @@ DirectMap1G: 11534336 kB

let fs = procfsapi::MockFS::new(files, pids, users, now);
let memtotal_kib = get_memtotal_kib(&fs).expect("Test: Must have data");
let (mut info, total_secs, per_cpu_secs) = get_process_information(&fs, memtotal_kib).expect("Test: Must have data");
let (mut info, total_secs, per_cpu_secs) =
get_process_information(&fs, memtotal_kib).expect("Test: Must have data");
assert!(info.len() == 1);
let mut xs = info.drain();
let p = xs.next().expect("Test: Should have data").1;
Expand Down Expand Up @@ -589,7 +591,7 @@ DirectMap1G: 11534336 kB
assert!(total_secs == (241155 + 582 + 127006 + 0 + 3816) / 100); // "cpu " line of "stat" data
assert!(per_cpu_secs.len() == 8);
assert!(per_cpu_secs[0] == (32528 + 189 + 19573 + 0 + 1149) / 100); // "cpu0 " line of "stat" data
assert!(per_cpu_secs[7] == (27582 + 61 + 12558 + 0 + 426) / 100); // "cpu7 " line of "stat" data
assert!(per_cpu_secs[7] == (27582 + 61 + 12558 + 0 + 426) / 100); // "cpu7 " line of "stat" data
}

#[test]
Expand Down Expand Up @@ -633,7 +635,8 @@ pub fn procfs_dead_and_undead_test() {

let fs = procfsapi::MockFS::new(files, pids, users, procfsapi::unix_now());
let memtotal_kib = get_memtotal_kib(&fs).expect("Test: Must have data");
let (mut info, _, _) = get_process_information(&fs, memtotal_kib).expect("Test: Must have data");
let (mut info, _, _) =
get_process_information(&fs, memtotal_kib).expect("Test: Must have data");

// 4020 should be dropped - it's dead
assert!(info.len() == 2);
Expand Down
14 changes: 7 additions & 7 deletions src/ps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -608,16 +608,16 @@ fn add_key(
for c in cards {
let v = extract(c);
if !first {
vs = vs + "|";
vs += "|";
}
if v != "" {
if !v.is_empty() {
any = true;
vs = vs + &v;
}
first = false;
}
if any {
if s != "" {
if !s.is_empty() {
s += ",";
}
s + key + "=" + &vs
Expand Down Expand Up @@ -780,12 +780,12 @@ fn print_record(
}
if params.opts.load {
if let Some(cpu_secs) = per_cpu_secs {
if cpu_secs.len() > 0 {
if !cpu_secs.is_empty() {
fields.push(format!("load={}", encode_cpu_secs_base45el(cpu_secs)));
}
}
if let Some(gpu_info) = gpu_info {
if gpu_info != "" {
if !gpu_info.is_empty() {
fields.push(format!("gpuinfo={gpu_info}"));
}
}
Expand Down Expand Up @@ -830,7 +830,7 @@ fn encode_cpu_secs_base45el(cpu_secs: &[u64]) -> String {
for x in cpu_secs {
s += encode_u64_base45el(*x - base).as_str();
}
return s;
s
}

// The only character unused by the encoding, other than the ones we're not allowed to use, is '='.
Expand All @@ -845,7 +845,7 @@ fn encode_u64_base45el(mut x: u64) -> String {
s.push(SUBSEQUENT[(x % BASE) as usize] as char);
x /= BASE;
}
return s;
s
}

#[test]
Expand Down
16 changes: 10 additions & 6 deletions src/slurmjobs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub fn show_slurm_jobs(window: &Option<u32>, span: &Option<String>) {
// Run sacct and parse the output.
match command::safe_command(
"sacct",
&vec![
&[
"-aP",
"-s",
&job_states.join(","),
Expand Down Expand Up @@ -128,10 +128,15 @@ fn check_ymd(s: &str) -> bool {
return false;
}
}
return k == 3;
k == 3
}

fn format_jobs(writer: &mut dyn io::Write, sacct_output: &str, field_names: &[&str], local: &libc::tm) {
fn format_jobs(
writer: &mut dyn io::Write,
sacct_output: &str,
field_names: &[&str],
local: &libc::tm,
) {
// Fields that are dates that may be reinterpreted before transmission.
let date_fields = HashSet::from(["Start", "End", "Submit"]);

Expand All @@ -155,9 +160,8 @@ fn format_jobs(writer: &mut dyn io::Write, sacct_output: &str, field_names: &[&s
let mut output_line = "v=".to_string() + VERSION;
for (i, name) in field_names.iter().enumerate() {
let mut val = fields[i].to_string();
let is_zero = val == ""
|| (!uncontrolled_fields.contains(name)
&& zero_values.contains(val.as_str()));
let is_zero = val.is_empty()
|| (!uncontrolled_fields.contains(name) && zero_values.contains(val.as_str()));
if !is_zero {
if date_fields.contains(name) {
// The slurm date format is localtime without a time zone offset. This
Expand Down
5 changes: 1 addition & 4 deletions src/sysinfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ fn do_show_system(
let mem_gib = (mem_by as f64 / GIB as f64).round() as i64;
let (mut cards, manufacturer) = match gpu::probe() {
Some(mut device) => (
match device.get_card_configuration() {
Ok(cards) => cards,
Err(_) => vec![],
},
device.get_card_configuration().unwrap_or_default(),
device.get_manufacturer(),
),
None => (vec![], "UNKNOWN".to_string()),
Expand Down
6 changes: 3 additions & 3 deletions src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ pub fn parse_date_and_time_no_tzo(s: &str) -> Result<libc::tm, String> {
}

fn parse_int_err(_e: ParseIntError) -> String {
return "Not an unsigned int value".to_string();
"Not an unsigned int value".to_string()
}

// Format a time as an ISO time stamp: yyyy-mm-ddThh:mm:ss+hh:mm
Expand Down Expand Up @@ -183,10 +183,10 @@ pub fn test_now_iso8601() {
#[test]
pub fn test_parse_date_and_time_no_tzo() {
let t = parse_date_and_time_no_tzo("2024-10-31T11:17").unwrap();
assert!(t.tm_year == 2024-1900 && t.tm_mon == 10-1 && t.tm_mday == 31);
assert!(t.tm_year == 2024 - 1900 && t.tm_mon == 10 - 1 && t.tm_mday == 31);
assert!(t.tm_hour == 11 && t.tm_min == 17);
let t = parse_date_and_time_no_tzo("2022-07-01T23:59:14").unwrap();
assert!(t.tm_year == 2022-1900 && t.tm_mon == 7-1 && t.tm_mday == 1);
assert!(t.tm_year == 2022 - 1900 && t.tm_mon == 7 - 1 && t.tm_mday == 1);
assert!(t.tm_hour == 23 && t.tm_min == 59 && t.tm_sec == 14);

assert!(parse_date_and_time_no_tzo("1969-07-01T23:59:14").is_err());
Expand Down

0 comments on commit 34e0cbb

Please sign in to comment.