use serde::{Deserialize, Serialize};
use std::fs::{self, File, OpenOptions};
use std::io::{Read, Seek, SeekFrom, Write};
use std::path::{Path, PathBuf};
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use crate::error::{IronError, Result};
const DEFAULT_MAX_SIZE: u64 = 100 * 1024 * 1024;
const MAGIC_BYTES: &[u8; 8] = b"SIMONDB\0";
const DB_VERSION: u32 = 7;
const HEADER_SIZE: u64 = 128;
pub fn parse_size(s: &str) -> Result<u64> {
let s = s.trim().to_uppercase();
let (num_str, multiplier) = if s.ends_with("GB") {
(&s[..s.len() - 2], 1024 * 1024 * 1024)
} else if s.ends_with("MB") {
(&s[..s.len() - 2], 1024 * 1024)
} else if s.ends_with("KB") {
(&s[..s.len() - 2], 1024)
} else if s.ends_with('B') {
(&s[..s.len() - 1], 1)
} else {
(s.as_str(), 1)
};
let num: u64 = num_str
.trim()
.parse()
.map_err(|_| IronError::Configuration(format!("Invalid size: {}", s)))?;
Ok(num * multiplier)
}
pub fn format_size(bytes: u64) -> String {
const KB: u64 = 1024;
const MB: u64 = KB * 1024;
const GB: u64 = MB * 1024;
if bytes >= GB {
format!("{:.2} GB", bytes as f64 / GB as f64)
} else if bytes >= MB {
format!("{:.2} MB", bytes as f64 / MB as f64)
} else if bytes >= KB {
format!("{:.2} KB", bytes as f64 / KB as f64)
} else {
format!("{} B", bytes)
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MetricSample {
pub timestamp: u64,
pub value: f64,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProcessSnapshot {
pub pid: u32,
pub name: String,
pub cpu_percent: Option<f32>,
pub memory_bytes: u64,
pub gpu_memory_bytes: Option<u64>,
pub gpu_percent: Option<f32>,
pub disk_read_bps: Option<u64>,
pub disk_write_bps: Option<u64>,
pub net_rx_bps: Option<u64>,
pub net_tx_bps: Option<u64>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SystemSnapshot {
pub timestamp: u64,
pub cpu_percent: Option<f32>,
pub cpu_per_core: Vec<Option<f32>>,
pub memory_used: Option<u64>,
pub memory_total: Option<u64>,
pub swap_used: Option<u64>,
pub swap_total: Option<u64>,
pub gpu_percent: Vec<Option<f32>>,
pub gpu_memory_used: Vec<Option<u64>>,
pub gpu_temperature: Vec<Option<f32>>,
pub gpu_power_mw: Vec<Option<u32>>,
pub net_rx_bps: Option<u64>,
pub net_tx_bps: Option<u64>,
pub processes: Vec<ProcessSnapshot>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
struct DatabaseHeader {
magic: [u8; 8],
version: u32,
max_size: u64,
data_size: u64,
record_count: u64,
first_timestamp: u64,
last_timestamp: u64,
_reserved: [u8; 16],
}
impl Default for DatabaseHeader {
fn default() -> Self {
let mut magic = [0u8; 8];
magic.copy_from_slice(MAGIC_BYTES);
Self {
magic,
version: DB_VERSION,
max_size: DEFAULT_MAX_SIZE,
data_size: 0,
record_count: 0,
first_timestamp: 0,
last_timestamp: 0,
_reserved: [0u8; 16],
}
}
}
#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[allow(dead_code)] enum RecordType {
System = 1,
Process = 2,
}
pub struct TimeSeriesDb {
path: PathBuf,
max_size: u64,
header: DatabaseHeader,
file: Option<File>,
}
impl TimeSeriesDb {
pub fn new<P: AsRef<Path>>(path: P, max_size: u64) -> Result<Self> {
let path = path.as_ref().to_path_buf();
let max_size = if max_size == 0 {
DEFAULT_MAX_SIZE
} else {
max_size
};
let mut db = Self {
path,
max_size,
header: DatabaseHeader::default(),
file: None,
};
db.header.max_size = max_size;
db.open_or_create()?;
Ok(db)
}
fn open_or_create(&mut self) -> Result<()> {
if self.path.exists() {
self.open_existing()?;
} else {
self.create_new()?;
}
Ok(())
}
fn open_existing(&mut self) -> Result<()> {
let mut file = OpenOptions::new()
.read(true)
.write(true)
.open(&self.path)
.map_err(|e| IronError::Other(e.to_string()))?;
let mut header_bytes = vec![0u8; HEADER_SIZE as usize];
file.read_exact(&mut header_bytes)
.map_err(|e| IronError::Other(e.to_string()))?;
let header: DatabaseHeader = bincode::deserialize(&header_bytes)
.map_err(|e| IronError::Configuration(format!("Invalid database header: {}", e)))?;
if &header.magic != MAGIC_BYTES {
return Err(IronError::Configuration(
"Invalid database file (magic bytes mismatch)".to_string(),
));
}
if header.version > DB_VERSION {
return Err(IronError::Configuration(format!(
"Database version {} is newer than supported version {}",
header.version, DB_VERSION
)));
}
if header.version < DB_VERSION {
return Err(IronError::Configuration(format!(
concat!(
"Database version {} predates version {}. Older files ",
"record an unreadable GPU figure as zero rather than as ",
"absent (before 3), Windows swap as the system commit ",
"charge rather than pagefile usage (before 4), and network ",
"rates as zero on every row because the reader could not ",
"compute one (before 5), and a just-started process's CPU ",
"as a figure over an assumed second or 0% (before 7). None ",
"can be converted, because a ",
"stored number does not say which definition produced it. ",
"Record to a new file, or delete this one with ",
"`ironmon record clear`.",
),
header.version, DB_VERSION
)));
}
self.header = header;
if self.max_size != DEFAULT_MAX_SIZE {
self.header.max_size = self.max_size;
}
self.file = Some(file);
Ok(())
}
fn create_new(&mut self) -> Result<()> {
if let Some(parent) = self.path.parent() {
fs::create_dir_all(parent).map_err(|e| IronError::Other(e.to_string()))?;
}
let mut file = OpenOptions::new()
.read(true)
.write(true)
.create(true)
.truncate(true)
.open(&self.path)
.map_err(|e| IronError::Other(e.to_string()))?;
self.write_header(&mut file)?;
self.file = Some(file);
Ok(())
}
fn write_header(&self, file: &mut File) -> Result<()> {
file.seek(SeekFrom::Start(0))
.map_err(|e| IronError::Other(e.to_string()))?;
let header_bytes = bincode::serialize(&self.header)
.map_err(|e| IronError::Other(format!("Failed to serialize header: {}", e)))?;
let mut padded = vec![0u8; HEADER_SIZE as usize];
padded[..header_bytes.len().min(HEADER_SIZE as usize)]
.copy_from_slice(&header_bytes[..header_bytes.len().min(HEADER_SIZE as usize)]);
file.write_all(&padded)
.map_err(|e| IronError::Other(e.to_string()))?;
Ok(())
}
pub fn record_system(&mut self, snapshot: &SystemSnapshot) -> Result<()> {
let data = bincode::serialize(snapshot)
.map_err(|e| IronError::Other(format!("Failed to serialize snapshot: {}", e)))?;
self.write_record(RecordType::System, &data, snapshot.timestamp)?;
Ok(())
}
fn write_record(&mut self, record_type: RecordType, data: &[u8], timestamp: u64) -> Result<()> {
let record_size = 1 + 4 + data.len() as u64;
let current_size = HEADER_SIZE + self.header.data_size;
if current_size + record_size > self.header.max_size {
self.rotate()?;
}
{
let file = self
.file
.as_mut()
.ok_or_else(|| IronError::Other("Database not open".to_string()))?;
file.seek(SeekFrom::Start(HEADER_SIZE + self.header.data_size))
.map_err(|e| IronError::Other(e.to_string()))?;
file.write_all(&[record_type as u8])
.map_err(|e| IronError::Other(e.to_string()))?;
let len_bytes = (data.len() as u32).to_le_bytes();
file.write_all(&len_bytes)
.map_err(|e| IronError::Other(e.to_string()))?;
file.write_all(data)
.map_err(|e| IronError::Other(e.to_string()))?;
}
self.header.data_size += record_size;
self.header.record_count += 1;
self.header.last_timestamp = timestamp;
if self.header.first_timestamp == 0 {
self.header.first_timestamp = timestamp;
}
self.flush_header()?;
Ok(())
}
fn flush_header(&mut self) -> Result<()> {
let file = self
.file
.as_mut()
.ok_or_else(|| IronError::Other("Database not open".to_string()))?;
file.seek(SeekFrom::Start(0))
.map_err(|e| IronError::Other(e.to_string()))?;
let header_bytes = bincode::serialize(&self.header)
.map_err(|e| IronError::Other(format!("Failed to serialize header: {}", e)))?;
let mut padded = vec![0u8; HEADER_SIZE as usize];
padded[..header_bytes.len().min(HEADER_SIZE as usize)]
.copy_from_slice(&header_bytes[..header_bytes.len().min(HEADER_SIZE as usize)]);
file.write_all(&padded)
.map_err(|e| IronError::Other(e.to_string()))?;
file.flush().map_err(|e| IronError::Other(e.to_string()))?;
Ok(())
}
fn rotate(&mut self) -> Result<()> {
log::info!(
"Database size limit reached ({} / {}), rotating...",
format_size(self.header.data_size),
format_size(self.header.max_size)
);
let records = self.read_all_system_snapshots()?;
let records_len = records.len();
let keep_count = records_len / 2;
let to_keep: Vec<_> = records.into_iter().skip(records_len - keep_count).collect();
self.file = None;
self.header = DatabaseHeader::default();
self.header.max_size = self.max_size;
self.create_new()?;
for snapshot in to_keep {
self.record_system(&snapshot)?;
}
log::info!(
"Rotation complete, kept {} records",
self.header.record_count
);
Ok(())
}
pub fn read_all_system_snapshots(&mut self) -> Result<Vec<SystemSnapshot>> {
let file = self
.file
.as_mut()
.ok_or_else(|| IronError::Other("Database not open".to_string()))?;
let mut snapshots = Vec::new();
file.seek(SeekFrom::Start(HEADER_SIZE))
.map_err(|e| IronError::Other(e.to_string()))?;
let mut offset = 0u64;
while offset < self.header.data_size {
let mut type_byte = [0u8; 1];
if file.read_exact(&mut type_byte).is_err() {
break;
}
let mut len_bytes = [0u8; 4];
if file.read_exact(&mut len_bytes).is_err() {
break;
}
let len = u32::from_le_bytes(len_bytes) as usize;
let mut data = vec![0u8; len];
if file.read_exact(&mut data).is_err() {
break;
}
offset += 1 + 4 + len as u64;
if type_byte[0] == RecordType::System as u8 {
if let Ok(snapshot) = bincode::deserialize::<SystemSnapshot>(&data) {
snapshots.push(snapshot);
}
}
}
Ok(snapshots)
}
pub fn query_range(&mut self, start_time: u64, end_time: u64) -> Result<Vec<SystemSnapshot>> {
let all = self.read_all_system_snapshots()?;
Ok(all
.into_iter()
.filter(|s| s.timestamp >= start_time && s.timestamp <= end_time)
.collect())
}
pub fn stats(&self) -> DatabaseStats {
DatabaseStats {
path: self.path.clone(),
max_size: self.header.max_size,
current_size: HEADER_SIZE + self.header.data_size,
record_count: self.header.record_count,
first_timestamp: if self.header.first_timestamp > 0 {
Some(self.header.first_timestamp)
} else {
None
},
last_timestamp: if self.header.last_timestamp > 0 {
Some(self.header.last_timestamp)
} else {
None
},
}
}
pub fn now_millis() -> u64 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or_default()
.as_millis() as u64
}
pub fn close(&mut self) -> Result<()> {
if let Some(mut file) = self.file.take() {
self.write_header(&mut file)?;
file.flush().map_err(|e| IronError::Other(e.to_string()))?;
}
Ok(())
}
}
impl Drop for TimeSeriesDb {
fn drop(&mut self) {
let _ = self.close();
}
}
#[derive(Debug, Clone)]
pub struct DatabaseStats {
pub path: PathBuf,
pub max_size: u64,
pub current_size: u64,
pub record_count: u64,
pub first_timestamp: Option<u64>,
pub last_timestamp: Option<u64>,
}
impl DatabaseStats {
pub fn usage_percent(&self) -> f32 {
(self.current_size as f32 / self.max_size as f32) * 100.0
}
pub fn time_span(&self) -> Option<String> {
if let (Some(first), Some(last)) = (self.first_timestamp, self.last_timestamp) {
let duration_ms = last.saturating_sub(first);
let duration = Duration::from_millis(duration_ms);
let secs = duration.as_secs();
if secs < 60 {
Some(format!("{} seconds", secs))
} else if secs < 3600 {
Some(format!("{} minutes", secs / 60))
} else if secs < 86400 {
Some(format!("{:.1} hours", secs as f64 / 3600.0))
} else {
Some(format!("{:.1} days", secs as f64 / 86400.0))
}
} else {
None
}
}
}
pub struct MetricsRecorder {
db: TimeSeriesDb,
interval: Duration,
#[allow(dead_code)] max_processes: usize,
recording: bool,
}
impl MetricsRecorder {
pub fn new<P: AsRef<Path>>(
path: P,
max_size: u64,
interval: Duration,
max_processes: usize,
) -> Result<Self> {
let db = TimeSeriesDb::new(path, max_size)?;
Ok(Self {
db,
interval,
max_processes,
recording: false,
})
}
pub fn stats(&self) -> DatabaseStats {
self.db.stats()
}
pub fn interval(&self) -> Duration {
self.interval
}
pub fn is_recording(&self) -> bool {
self.recording
}
pub fn record_snapshot(&mut self, snapshot: SystemSnapshot) -> Result<()> {
self.db.record_system(&snapshot)
}
pub fn get_all_snapshots(&mut self) -> Result<Vec<SystemSnapshot>> {
self.db.read_all_system_snapshots()
}
pub fn query_range(&mut self, start: u64, end: u64) -> Result<Vec<SystemSnapshot>> {
self.db.query_range(start, end)
}
pub fn close(&mut self) -> Result<()> {
self.recording = false;
self.db.close()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_parse_size() {
assert_eq!(parse_size("100").unwrap(), 100);
assert_eq!(parse_size("100B").unwrap(), 100);
assert_eq!(parse_size("100KB").unwrap(), 100 * 1024);
assert_eq!(parse_size("100MB").unwrap(), 100 * 1024 * 1024);
assert_eq!(parse_size("1GB").unwrap(), 1024 * 1024 * 1024);
assert_eq!(parse_size("500mb").unwrap(), 500 * 1024 * 1024);
assert_eq!(parse_size(" 100 MB ").unwrap(), 100 * 1024 * 1024);
}
#[test]
fn test_format_size() {
assert_eq!(format_size(500), "500 B");
assert_eq!(format_size(1024), "1.00 KB");
assert_eq!(format_size(1024 * 1024), "1.00 MB");
assert_eq!(format_size(1024 * 1024 * 1024), "1.00 GB");
}
#[test]
fn an_absent_sensor_survives_the_round_trip_as_absent() {
let dir = std::env::temp_dir().join(format!(
"ironmon-tsdb-absence-{}-{:?}",
std::process::id(),
std::thread::current().id()
));
std::fs::create_dir_all(&dir).expect("temp dir");
let path = dir.join("m.db");
let _ = std::fs::remove_file(&path);
let written = SystemSnapshot {
timestamp: TimeSeriesDb::now_millis(),
cpu_percent: Some(12.5),
cpu_per_core: vec![Some(10.0), Some(15.0)],
memory_used: Some(8_000_000_000),
memory_total: Some(16_000_000_000),
swap_used: Some(0),
swap_total: Some(0),
gpu_percent: vec![Some(44.0), Some(0.0), None],
gpu_memory_used: vec![Some(1024), Some(0), None],
gpu_temperature: vec![Some(61.0), Some(0.0), None],
gpu_power_mw: vec![Some(150_000), Some(0), None],
net_rx_bps: Some(1),
net_tx_bps: Some(2),
processes: vec![ProcessSnapshot {
pid: 1,
name: "p".to_string(),
cpu_percent: Some(1.0),
memory_bytes: 2,
gpu_memory_bytes: Some(3),
gpu_percent: None,
disk_read_bps: None,
disk_write_bps: None,
net_rx_bps: None,
net_tx_bps: None,
}],
};
{
let mut db = TimeSeriesDb::new(&path, 1024 * 1024).expect("open");
db.record_system(&written).expect("write");
db.close().expect("close");
}
let mut db = TimeSeriesDb::new(&path, 1024 * 1024).expect("reopen");
let read = db.read_all_system_snapshots().expect("read");
let _ = std::fs::remove_file(&path);
assert_eq!(read.len(), 1, "one record was written");
let s = &read[0];
assert_eq!(s.gpu_temperature[0], Some(61.0), "a measured temperature");
assert_eq!(
s.gpu_temperature[1],
Some(0.0),
"a genuine zero stays a zero"
);
assert_eq!(
s.gpu_temperature[2], None,
"an adapter with no sensor must not read back as zero degrees"
);
assert_ne!(
s.gpu_temperature[1], s.gpu_temperature[2],
"zero and unreadable are different facts"
);
assert_eq!(
s.gpu_power_mw[2], None,
"no power telemetry is not zero watts"
);
assert_eq!(
s.gpu_percent[2], None,
"an unqueried GPU is not an idle one"
);
assert_eq!(s.gpu_memory_used[2], None);
let p = &s.processes[0];
assert_eq!(p.net_rx_bps, None, "untracked per-process I/O is not zero");
assert_eq!(p.disk_read_bps, None);
assert_eq!(p.gpu_percent, None);
assert_eq!(s.cpu_percent, Some(12.5));
assert_eq!(s.cpu_per_core, vec![Some(10.0), Some(15.0)]);
assert_eq!(
s.swap_used,
Some(0),
"a machine with an idle pagefile really is at zero"
);
}
#[test]
fn a_tick_that_read_nothing_reads_back_as_nothing() {
let dir = std::env::temp_dir().join(format!(
"ironmon-tsdb-unread-{}-{:?}",
std::process::id(),
std::thread::current().id()
));
std::fs::create_dir_all(&dir).expect("temp dir");
let path = dir.join("m.db");
let _ = std::fs::remove_file(&path);
let written = SystemSnapshot {
timestamp: TimeSeriesDb::now_millis(),
cpu_percent: None,
cpu_per_core: vec![None, None],
memory_used: None,
memory_total: None,
swap_used: None,
swap_total: None,
gpu_percent: Vec::new(),
gpu_memory_used: Vec::new(),
gpu_temperature: Vec::new(),
gpu_power_mw: Vec::new(),
net_rx_bps: None,
net_tx_bps: None,
processes: Vec::new(),
};
{
let mut db = TimeSeriesDb::new(&path, 1024 * 1024).expect("open");
db.record_system(&written).expect("write");
db.close().expect("close");
}
let mut db = TimeSeriesDb::new(&path, 1024 * 1024).expect("reopen");
let read = db.read_all_system_snapshots().expect("read");
assert_eq!(read.len(), 1);
let s = &read[0];
assert_eq!(s.cpu_percent, None, "an unread CPU is not an idle one");
assert_eq!(s.cpu_per_core, vec![None, None]);
assert_eq!(s.memory_used, None);
assert_eq!(
s.memory_total, None,
"a zero here would make the readback divide by it"
);
assert_eq!(
s.swap_used, None,
"an unread pagefile is not an unused pagefile"
);
assert_eq!(s.swap_total, None);
let _ = std::fs::remove_file(&path);
}
}