Skip to content

Commit

Permalink
Format
Browse files Browse the repository at this point in the history
  • Loading branch information
Lars T Hansen committed Jun 20, 2024
1 parent 5f0c74f commit 3bfbd41
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
6 changes: 3 additions & 3 deletions src/procfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,10 @@ pub fn get_process_information(
let cpu_no: usize;
match fields[0][3..].parse::<usize>() {
Ok(x) => cpu_no = x,
Err(_) => return Err("Bad cpu field".to_string())
Err(_) => return Err("Bad cpu field".to_string()),
}
if per_cpu_ticks.len() < cpu_no+1 {
per_cpu_ticks.resize(cpu_no+1, 0u64);
if per_cpu_ticks.len() < cpu_no + 1 {
per_cpu_ticks.resize(cpu_no + 1, 0u64);
}
per_cpu_ticks[cpu_no] = sum;
}
Expand Down
40 changes: 30 additions & 10 deletions src/ps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,16 @@ fn do_create_snapshot(jobs: &mut dyn jobs::JobManager, opts: &PsOptions, timesta

let mut did_print = false;
for c in candidates {
match print_record(&mut writer, &print_params, &c, if !did_print { Some(&per_cpu_ticks) } else { None }) {
match print_record(
&mut writer,
&print_params,
&c,
if !did_print {
Some(&per_cpu_ticks)
} else {
None
},
) {
Ok(did_print_one) => did_print = did_print_one || did_print,
Err(_) => {
// Discard the error: there's nothing very sensible we can do at this point if the
Expand Down Expand Up @@ -551,7 +560,16 @@ fn do_create_snapshot(jobs: &mut dyn jobs::JobManager, opts: &PsOptions, timesta
gpu_status: GpuStatus::Ok,
};
// Discard the error, see above.
let _ = print_record(&mut writer, &print_params, &synth, if !did_print { Some(&per_cpu_ticks) } else { None });
let _ = print_record(
&mut writer,
&print_params,
&synth,
if !did_print {
Some(&per_cpu_ticks)
} else {
None
},
);
}

// Discard the error code, see above.
Expand Down Expand Up @@ -738,18 +756,21 @@ fn print_record(
// cpu_info must be nonempty and the values in it are ticks.

fn encode_cpu_info_base45el(cpu_info: &[u64], ticks_per_sec: u64) -> String {
let base = *cpu_info.iter().reduce(|a, b| std::cmp::min(a,b)).expect("Non-empty");
let mut s = encode_u64_base45el(base/ticks_per_sec);
let base = *cpu_info
.iter()
.reduce(|a, b| std::cmp::min(a, b))
.expect("Non-empty");
let mut s = encode_u64_base45el(base / ticks_per_sec);
for x in cpu_info {
s += encode_u64_base45el((*x - base)/ticks_per_sec).as_str();
s += encode_u64_base45el((*x - base) / ticks_per_sec).as_str();
}
return s;
}

// Unused by the encoding: '='
const BASE : u64 = 45;
const INITIAL : &[u8] = "(){}[]<>+-abcdefghijklmnopqrstuvwxyz!@#$%^&*_".as_bytes();
const SUBSEQUENT : &[u8] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ~|';:.?/`".as_bytes();
const BASE: u64 = 45;
const INITIAL: &[u8] = "(){}[]<>+-abcdefghijklmnopqrstuvwxyz!@#$%^&*_".as_bytes();
const SUBSEQUENT: &[u8] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ~|';:.?/`".as_bytes();

fn encode_u64_base45el(mut x: u64) -> String {
if x == 0 {
Expand All @@ -767,8 +788,7 @@ fn encode_u64_base45el(mut x: u64) -> String {
#[test]
pub fn test_encoding() {
// this should be *1, *0, *29, *43, 1, *11 with * denoting an initial
let v = vec![1,30,89,12];
let v = vec![1, 30, 89, 12];
println!("{}", encode_cpu_info_base45el(&v, 1));
assert!(encode_cpu_info_base45el(&v, 1) == ")(t*1b");
}

0 comments on commit 3bfbd41

Please sign in to comment.