Skip to content

Commit

Permalink
For NordicHPC#152 - Remove serde_json dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
Lars T Hansen committed Apr 4, 2024
1 parent f2f229a commit 432d073
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 50 deletions.
13 changes: 0 additions & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,3 @@ env_logger = "0.11"
page_size = "0.6"
libc = "0.2"
signal-hook = "0.3"
serde_json = "1.0.114"
serde = { version = "1.0.197", features = ["derive"] }
58 changes: 23 additions & 35 deletions src/sysinfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ use crate::gpu;
use crate::nvidia;
use crate::procfs;
use crate::procfsapi;

use serde::Serialize;
use crate::util;

use std::io::{self, Write};

Expand All @@ -22,21 +21,6 @@ pub fn show_system(timestamp: &str) {

const GIB: usize = 1024 * 1024 * 1024;

// Note the field names here are used by decoders developed separately and should be considered set
// in stone. All fields will be serialized; missing numeric values must be zero; consumers must
// deal with that.

#[derive(Serialize)]
struct NodeConfig {
timestamp: String,
hostname: String,
description: String,
cpu_cores: i32,
mem_gb: i64, // This is GiB despite the name - the name is frozen
gpu_cards: i32,
gpumem_gb: i64, // Ditto
}

fn do_show_system(fs: &dyn procfsapi::ProcfsAPI, timestamp: &str) -> Result<(), String> {
let (model, sockets, cores_per_socket, threads_per_core) = procfs::get_cpu_info(fs)?;
let mem_by = procfs::get_memtotal_kib(fs)? * 1024;
Expand Down Expand Up @@ -90,22 +74,26 @@ fn do_show_system(fs: &dyn procfsapi::ProcfsAPI, timestamp: &str) -> Result<(),
} else {
("".to_string(), 0, 0)
};
let config = NodeConfig {
timestamp: timestamp.to_string(),
hostname,
description: format!("{sockets}x{cores_per_socket}{ht} {model}, {mem_gib} GiB{gpu_desc}"),
cpu_cores: sockets * cores_per_socket * threads_per_core,
mem_gb: mem_gib,
gpu_cards,
gpumem_gb,
};
match serde_json::to_string_pretty(&config) {
Ok(s) => {
let _ = io::stdout().write(s.as_bytes());
let _ = io::stdout().write(b"\n");
let _ = io::stdout().flush();
Ok(())
}
Err(_) => Err("JSON encoding failed".to_string()),
}
let timestamp = util::json_quote(timestamp);
let hostname = util::json_quote(&hostname);
let description = util::json_quote(&format!("{sockets}x{cores_per_socket}{ht} {model}, {mem_gib} GiB{gpu_desc}"));
let cpu_cores = sockets * cores_per_socket * threads_per_core;

// Note the field names here are used by decoders that are developed separately, and they should
// be considered set in stone.

let s = format!(r#"{{
"timestamp": "{timestamp}",
"hostname": "{hostname}",
"description": "{description}",
"cpu_cores": {cpu_cores},
"mem_gb": {mem_gib},
"gpu_cards": {gpu_cards},
"gpumem_gb": {gpumem_gb}
}}
"#);

let _ = io::stdout().write(s.as_bytes());
let _ = io::stdout().flush();
Ok(())
}
31 changes: 31 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,34 @@ pub fn chunks(input: &str) -> (Vec<usize>, Vec<&str>) {
pub fn three_places(n: f64) -> f64 {
(n * 1000.0).round() / 1000.0
}

// Insert \ before " and \
// Insert escape sequences for well-known control chars.
// Translate all other control chars to spaces (it's possible to do better).
pub fn json_quote(s: &str) -> String {
let mut t = "".to_string();
for c in s.chars() {
match c {
'"' | '\\' => {
t.push('\\');
t.push(c);
}
'\n' => {
t.push_str("\\n");
}
'\r' => {
t.push_str("\\r");
}
'\t' => {
t.push_str("\\t");
}
_ctl if c < ' ' => {
t.push(' ');
}
_ => {
t.push(c);
}
}
}
t
}

0 comments on commit 432d073

Please sign in to comment.