Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

For #152 - Remove serde_json dependency #154

Merged
merged 3 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 0 additions & 45 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 @@ -12,5 +12,3 @@ log = "0.4"
env_logger = "0.11"
libc = "0.2"
signal-hook = { version = "0.3", default-features = false, features = [] }
serde_json = "1.0.114"
serde = { version = "1.0.197", features = ["derive"] }
71 changes: 33 additions & 38 deletions src/sysinfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ use crate::hostname;
use crate::nvidia;
use crate::procfs;
use crate::procfsapi;
use crate::util;

use serde::Serialize;

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

pub fn show_system(timestamp: &str) {
let fs = procfsapi::RealFS::new();
match do_show_system(&fs, timestamp) {
let mut writer = io::stdout();
match do_show_system(&mut writer, &fs, timestamp) {
Ok(_) => {}
Err(e) => {
log::error!("sysinfo failed: {e}");
Expand All @@ -23,22 +23,7 @@ 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> {
fn do_show_system(writer: &mut dyn io::Write, 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;
let mem_gib = (mem_by as f64 / GIB as f64).round() as i64;
Expand Down Expand Up @@ -91,22 +76,32 @@ 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}
}}
"#);

// Ignore I/O errors.

let _ = writer.write(s.as_bytes());
let _ = writer.flush();
Ok(())
}

// Currently the test for do_show_system() is black-box, see ../tests. The reason for this is partly
// that not all the system interfaces used by that function are virtualized at this time, and partly
// that we only care that the output syntax looks right.
42 changes: 42 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,48 @@ 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
}

#[test]
pub fn json_quote_test() {
assert!(&json_quote("abcde") == "abcde");
assert!(&json_quote(r#"abc\de"#) == r#"abc\\de"#);
assert!(&json_quote(r#"abc"de"#) == r#"abc\"de"#);
assert!(&json_quote("abc\nde") == r#"abc\nde"#);
assert!(&json_quote("abc\rde") == r#"abc\rde"#);
assert!(&json_quote("abc de") == r#"abc\tde"#);
assert!(&json_quote("abc\u{0008}de") == r#"abc de"#);
}

// If the value contains a , or " then quote the string, and double every "
pub fn csv_quote(s: &str) -> String {
let mut t = "".to_string();
Expand Down
13 changes: 13 additions & 0 deletions tests/sysinfo-syntax.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash
#
# Check that sysinfo produces properly formatted JSON.
# Requirement: the `jq` utility.

set -e
( cd .. ; cargo build )
if [[ $(command -v jq) == "" ]]; then
echo "Install jq first"
exit 1
fi
output=$(../target/debug/sonar sysinfo)
jq . <<< $output > /dev/null
Loading