| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
use std::io::Write; |
| 18 |
use std::time::{SystemTime, UNIX_EPOCH}; |
| 19 |
|
| 20 |
use serde_json::{Map, Value}; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
const SCHEMA: u32 = 1; |
| 25 |
|
| 26 |
pub(crate) struct Report { |
| 27 |
mode: &'static str, |
| 28 |
metrics: Map<String, Value>, |
| 29 |
storage: Map<String, Value>, |
| 30 |
} |
| 31 |
|
| 32 |
impl Report { |
| 33 |
pub(crate) fn new(mode: &'static str) -> Self { |
| 34 |
Self { |
| 35 |
mode, |
| 36 |
metrics: Map::new(), |
| 37 |
storage: Map::new(), |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
pub(crate) fn set_storage(&mut self, role: &str, storage: &crate::storage::Storage) { |
| 48 |
self.storage.insert(role.to_string(), storage.to_json()); |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
pub(crate) fn set(&mut self, key: &str, value: impl Into<Value>) { |
| 54 |
self.metrics.insert(key.to_string(), value.into()); |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
pub(crate) fn checkpoint(&self) { |
| 70 |
static REPORTED: std::sync::Once = std::sync::Once::new(); |
| 71 |
if let Err(e) = self.write_json() { |
| 72 |
REPORTED.call_once(|| eprintln!("\n could not checkpoint AF_BENCH_JSON: {e}")); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
pub(crate) fn write(&self) { |
| 79 |
if std::env::var("AF_BENCH_JSON").is_err() { |
| 80 |
return; |
| 81 |
} |
| 82 |
match self.write_json() { |
| 83 |
Ok(Some(path)) => println!("\n wrote {path}"), |
| 84 |
Ok(None) => {} |
| 85 |
|
| 86 |
Err(e) => eprintln!("\n could not write AF_BENCH_JSON: {e}"), |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
fn write_json(&self) -> std::io::Result<Option<String>> { |
| 97 |
let Ok(path) = std::env::var("AF_BENCH_JSON") else { |
| 98 |
return Ok(None); |
| 99 |
}; |
| 100 |
let mut root = Map::new(); |
| 101 |
root.insert("schema".into(), SCHEMA.into()); |
| 102 |
root.insert("mode".into(), self.mode.into()); |
| 103 |
|
| 104 |
|
| 105 |
if let Ok(d) = SystemTime::now().duration_since(UNIX_EPOCH) { |
| 106 |
root.insert("unix_time".into(), d.as_secs().into()); |
| 107 |
} |
| 108 |
if let Some(rss) = peak_rss_mb() { |
| 109 |
root.insert("peak_rss_mb".into(), rss.into()); |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
if !self.storage.is_empty() { |
| 115 |
root.insert("storage".into(), Value::Object(self.storage.clone())); |
| 116 |
} |
| 117 |
if let Some(mem) = crate::storage::mem_total_mb() { |
| 118 |
root.insert("mem_total_mb".into(), mem.round().into()); |
| 119 |
} |
| 120 |
root.insert("metrics".into(), Value::Object(self.metrics.clone())); |
| 121 |
|
| 122 |
let text = serde_json::to_string_pretty(&Value::Object(root)).unwrap_or_default(); |
| 123 |
std::fs::File::create(&path).and_then(|mut f| f.write_all(text.as_bytes()))?; |
| 124 |
Ok(Some(path)) |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
pub(crate) fn peak_rss_mb() -> Option<f64> { |
| 135 |
let status = std::fs::read_to_string("/proc/self/status").ok()?; |
| 136 |
let line = status.lines().find(|l| l.starts_with("VmHWM:"))?; |
| 137 |
let kb: f64 = line.split_whitespace().nth(1)?.parse().ok()?; |
| 138 |
Some(kb / 1024.0) |
| 139 |
} |
| 140 |
|