PR-07 telemetry + certs
This commit is contained in:
@@ -5,23 +5,81 @@ mod log_sink;
|
||||
|
||||
use crate::prometeu_runner::PrometeuRunner;
|
||||
use winit::event_loop::EventLoop;
|
||||
use prometeu_core::telemetry::CertificationConfig;
|
||||
|
||||
fn load_cap_config(path: &str) -> Option<CertificationConfig> {
|
||||
let content = std::fs::read_to_string(path).ok()?;
|
||||
let mut config = CertificationConfig {
|
||||
enabled: true,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
for line in content.lines() {
|
||||
let line = line.trim();
|
||||
if line.is_empty() || line.starts_with('#') { continue; }
|
||||
let parts: Vec<&str> = line.split('=').collect();
|
||||
if parts.len() != 2 { continue; }
|
||||
let key = parts[0].trim();
|
||||
let val = parts[1].trim();
|
||||
|
||||
match key {
|
||||
"cycles_budget" => config.cycles_budget_per_frame = val.parse().ok(),
|
||||
"max_syscalls" => config.max_syscalls_per_frame = val.parse().ok(),
|
||||
"max_host_cpu_us" => config.max_host_cpu_us_per_frame = val.parse().ok(),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
Some(config)
|
||||
}
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let args: Vec<String> = std::env::args().collect();
|
||||
let mut fs_root = None;
|
||||
let mut cap_config = None;
|
||||
|
||||
let mut i = 0;
|
||||
while i < args.len() {
|
||||
if args[i] == "--fs-root" && i + 1 < args.len() {
|
||||
fs_root = Some(args[i + 1].clone());
|
||||
i += 1;
|
||||
} else if args[i] == "--cap" && i + 1 < args.len() {
|
||||
cap_config = load_cap_config(&args[i + 1]);
|
||||
i += 1;
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
|
||||
let event_loop = EventLoop::new()?;
|
||||
|
||||
let mut runner = PrometeuRunner::new(fs_root);
|
||||
let mut runner = PrometeuRunner::new(fs_root, cap_config);
|
||||
event_loop.run_app(&mut runner)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_load_cap_config() {
|
||||
let content = "cycles_budget=500\nmax_syscalls=10\n# comentário\nmax_host_cpu_us=2000";
|
||||
let path = "test_cap.cfg";
|
||||
std::fs::write(path, content).unwrap();
|
||||
|
||||
let config = load_cap_config(path).unwrap();
|
||||
assert!(config.enabled);
|
||||
assert_eq!(config.cycles_budget_per_frame, Some(500));
|
||||
assert_eq!(config.max_syscalls_per_frame, Some(10));
|
||||
assert_eq!(config.max_host_cpu_us_per_frame, Some(2000));
|
||||
|
||||
std::fs::remove_file(path).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_load_cap_config_not_found() {
|
||||
let config = load_cap_config("non_existent.cfg");
|
||||
assert!(config.is_none());
|
||||
}
|
||||
}
|
||||
@@ -17,6 +17,8 @@ use winit::event_loop::{ActiveEventLoop, ControlFlow};
|
||||
use winit::keyboard::{KeyCode, PhysicalKey};
|
||||
use winit::window::{Window, WindowAttributes, WindowId};
|
||||
|
||||
use prometeu_core::telemetry::CertificationConfig;
|
||||
|
||||
pub struct PrometeuRunner {
|
||||
window: Option<&'static Window>,
|
||||
pixels: Option<Pixels<'static>>,
|
||||
@@ -35,6 +37,10 @@ pub struct PrometeuRunner {
|
||||
|
||||
last_stats_update: Instant,
|
||||
frames_since_last_update: u64,
|
||||
current_fps: f64,
|
||||
|
||||
overlay_enabled: bool,
|
||||
|
||||
audio_load_accum_us: u64,
|
||||
audio_load_samples: u64,
|
||||
audio_perf_consumer: Option<ringbuf::wrap::CachingCons<Arc<HeapRb<u64>>>>,
|
||||
@@ -44,10 +50,10 @@ pub struct PrometeuRunner {
|
||||
}
|
||||
|
||||
impl PrometeuRunner {
|
||||
pub(crate) fn new(fs_root: Option<String>) -> Self {
|
||||
pub(crate) fn new(fs_root: Option<String>, cap_config: Option<CertificationConfig>) -> Self {
|
||||
let target_fps = 60;
|
||||
|
||||
let mut firmware = Firmware::new();
|
||||
let mut firmware = Firmware::new(cap_config);
|
||||
if let Some(root) = &fs_root {
|
||||
let backend = HostDirBackend::new(root);
|
||||
firmware.os.mount_fs(Box::new(backend));
|
||||
@@ -67,6 +73,8 @@ impl PrometeuRunner {
|
||||
|
||||
last_stats_update: Instant::now(),
|
||||
frames_since_last_update: 0,
|
||||
current_fps: 0.0,
|
||||
overlay_enabled: false,
|
||||
audio_load_accum_us: 0,
|
||||
audio_load_samples: 0,
|
||||
audio_perf_consumer: None,
|
||||
@@ -227,6 +235,12 @@ impl ApplicationHandler for PrometeuRunner {
|
||||
KeyCode::KeyZ => self.input_signals.start_signal = is_down,
|
||||
KeyCode::ShiftLeft | KeyCode::ShiftRight => self.input_signals.select_signal = is_down,
|
||||
|
||||
KeyCode::F1 => {
|
||||
if is_down {
|
||||
self.overlay_enabled = !self.overlay_enabled;
|
||||
}
|
||||
}
|
||||
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
@@ -304,8 +318,8 @@ impl ApplicationHandler for PrometeuRunner {
|
||||
// Atualiza estatísticas a cada 1 segundo real
|
||||
let stats_elapsed = now.duration_since(self.last_stats_update);
|
||||
if stats_elapsed >= Duration::from_secs(1) {
|
||||
self.current_fps = self.frames_since_last_update as f64 / stats_elapsed.as_secs_f64();
|
||||
if let Some(window) = self.window {
|
||||
let fps = self.frames_since_last_update as f64 / stats_elapsed.as_secs_f64();
|
||||
let kb = self.hardware.gfx.memory_usage_bytes() as f64 / 1024.0;
|
||||
|
||||
// comparação fixa sempre contra 60Hz, manter mesmo quando fazer teste de stress na CPU
|
||||
@@ -321,7 +335,7 @@ impl ApplicationHandler for PrometeuRunner {
|
||||
|
||||
let title = format!(
|
||||
"PROMETEU | GFX: {:.1} KB | FPS: {:.1} | Load: {:.1}% (C) + {:.1}% (A) | Frame: tick {} logical {}",
|
||||
kb, fps, cpu_load_core, cpu_load_audio, self.firmware.os.tick_index, self.firmware.os.logical_frame_index
|
||||
kb, self.current_fps, cpu_load_core, cpu_load_audio, self.firmware.os.tick_index, self.firmware.os.logical_frame_index
|
||||
);
|
||||
window.set_title(&title);
|
||||
}
|
||||
@@ -341,6 +355,36 @@ impl ApplicationHandler for PrometeuRunner {
|
||||
};
|
||||
self.log_sink.process_events(new_events);
|
||||
|
||||
// Overlay de Telemetria
|
||||
if self.overlay_enabled {
|
||||
self.hardware.gfx.present(); // Traz o front para o back para desenhar por cima
|
||||
|
||||
let tel = &self.firmware.os.telemetry_last;
|
||||
let color_text = prometeu_core::model::Color::WHITE;
|
||||
let color_bg = prometeu_core::model::Color::INDIGO; // Azul escuro para destacar
|
||||
let color_warn = prometeu_core::model::Color::RED;
|
||||
|
||||
self.hardware.gfx.fill_rect(5, 5, 140, 65, color_bg);
|
||||
self.hardware.gfx.draw_text(10, 10, &format!("FPS: {:.1}", self.current_fps), color_text);
|
||||
self.hardware.gfx.draw_text(10, 18, &format!("HOST: {:.2}MS", tel.host_cpu_time_us as f64 / 1000.0), color_text);
|
||||
self.hardware.gfx.draw_text(10, 26, &format!("STEPS: {}", tel.vm_steps), color_text);
|
||||
self.hardware.gfx.draw_text(10, 34, &format!("SYSC: {}", tel.syscalls), color_text);
|
||||
self.hardware.gfx.draw_text(10, 42, &format!("CYC: {}", tel.cycles_used), color_text);
|
||||
|
||||
let cert_color = if tel.violations > 0 { color_warn } else { color_text };
|
||||
self.hardware.gfx.draw_text(10, 50, &format!("CERT LAST: {}", tel.violations), cert_color);
|
||||
|
||||
if tel.violations > 0 {
|
||||
if let Some(event) = self.firmware.os.log_service.get_recent(10).into_iter().rev().find(|e| e.tag >= 0xCA01 && e.tag <= 0xCA03) {
|
||||
let mut msg = event.msg.clone();
|
||||
if msg.len() > 30 { msg.truncate(30); }
|
||||
self.hardware.gfx.draw_text(10, 58, &msg, color_warn);
|
||||
}
|
||||
}
|
||||
|
||||
self.hardware.gfx.present(); // Devolve para o front com o overlay aplicado
|
||||
}
|
||||
|
||||
self.request_redraw();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user