feat(runtime): add host wall clock and administrative UTC timestamps
Intrepid/Prometeu/Runtime/pipeline/pr-master This commit looks good

This commit is contained in:
2026-10-13 09:16:15 +01:00
parent 3e39a90083
commit 23dc6fc66d
21 changed files with 603 additions and 51 deletions
@@ -24,3 +24,7 @@ cpal = "0.15.3"
ringbuf = "0.4.7"
serde_json = "1.0.149"
clap = { version = "4.5", features = ["derive"] }
[[bench]]
name = "wall_clock"
harness = false
@@ -0,0 +1,37 @@
use prometeu_drivers::ControlledWallClock;
use prometeu_hal::WallClock;
use prometeu_host_desktop_winit::wall_clock::SystemWallClock;
use std::hint::black_box;
use std::time::Instant;
fn measure(name: &str, clock: &dyn WallClock) {
const BATCHES: usize = 101;
const READS: usize = 10_000;
for _ in 0..READS {
black_box(black_box(clock).utc_unix_seconds());
}
let mut samples = Vec::with_capacity(BATCHES);
for _ in 0..BATCHES {
let start = Instant::now();
for _ in 0..READS {
black_box(black_box(clock).utc_unix_seconds());
}
samples.push(start.elapsed().as_nanos() as f64 / READS as f64);
}
samples.sort_by(f64::total_cmp);
println!(
"{name}: batches={BATCHES} reads_per_batch={READS} ns/read batch_mean p50={:.2} p95={:.2} max={:.2}",
samples[50], samples[95], samples[100]
);
}
fn main() {
println!(
"target={}-{} debug_assertions={}; batch means include dynamic dispatch and loop overhead",
std::env::consts::ARCH,
std::env::consts::OS,
cfg!(debug_assertions)
);
measure("system", &SystemWallClock);
measure("controlled", &ControlledWallClock::new(1_000));
}
@@ -7,6 +7,7 @@ pub mod log_sink;
pub mod runner;
pub mod stats;
pub mod utilities;
pub mod wall_clock;
use cap::load_cap_config;
use clap::Parser;
@@ -163,7 +163,7 @@ impl HostRunner {
}
pub(crate) fn set_games_root(&mut self, games_root: String) {
let library = discover_games_root(&games_root);
let library = discover_games_root(&games_root, self.hardware.wall_clock());
for diagnostic in library.diagnostics() {
eprintln!(
"games-root: omitted candidate {}: {:?}",
@@ -185,7 +185,10 @@ impl HostRunner {
}
let memory_banks = Arc::new(MemoryBanks::new());
let hardware = Hardware::new_with_memory_banks(Arc::clone(&memory_banks));
let hardware = Hardware::new_with_memory_banks(
Arc::clone(&memory_banks),
Arc::new(crate::wall_clock::SystemWallClock),
);
let display_size = hardware.display_size();
let worker_backend = LocalFramebufferRenderBackend::new_with_memory_banks(
display_size.0,
@@ -0,0 +1,52 @@
use prometeu_hal::WallClock;
use std::time::{SystemTime, UNIX_EPOCH};
/// Desktop civil time. Synchronization and clock adjustments belong to the OS.
#[derive(Debug, Default)]
pub struct SystemWallClock;
impl WallClock for SystemWallClock {
fn utc_unix_seconds(&self) -> u64 {
seconds_from_system_time(SystemTime::now())
}
}
fn seconds_from_system_time(time: SystemTime) -> u64 {
match time.duration_since(UNIX_EPOCH) {
Ok(duration) => seconds_from_unix_nanos(duration.as_nanos()),
Err(_) => 0,
}
}
fn seconds_from_unix_nanos(nanos: u128) -> u64 {
u64::try_from(nanos / 1_000_000_000).unwrap_or(u64::MAX)
}
#[cfg(test)]
mod tests {
use super::*;
use std::time::Duration;
#[test]
fn conversion_truncates_fractions_and_saturates_at_both_limits() {
assert_eq!(seconds_from_system_time(UNIX_EPOCH), 0);
assert_eq!(seconds_from_system_time(UNIX_EPOCH - Duration::from_nanos(1)), 0);
assert_eq!(seconds_from_system_time(UNIX_EPOCH - Duration::from_secs(1)), 0);
assert_eq!(seconds_from_system_time(UNIX_EPOCH + Duration::from_millis(999)), 0);
assert_eq!(seconds_from_system_time(UNIX_EPOCH + Duration::from_millis(1_900)), 1);
let limit = u128::from(u64::MAX) * 1_000_000_000;
assert_eq!(seconds_from_unix_nanos(limit - 1), u64::MAX - 1);
assert_eq!(seconds_from_unix_nanos(limit), u64::MAX);
assert_eq!(seconds_from_unix_nanos(limit + 1_000_000_000), u64::MAX);
assert_eq!(seconds_from_unix_nanos(u128::MAX), u64::MAX);
}
#[test]
fn system_provider_reads_current_local_time() {
let before = seconds_from_system_time(SystemTime::now());
let actual = SystemWallClock.utc_unix_seconds();
let after = seconds_from_system_time(SystemTime::now());
// A wall-clock adjustment may reverse the bounding samples.
assert!((before.min(after)..=before.max(after)).contains(&actual));
}
}