PR-07 telemetry + certs
This commit is contained in:
@@ -3,6 +3,7 @@ use crate::hardware::{HardwareBridge, InputSignals};
|
||||
use crate::log::{LogLevel, LogService, LogSource};
|
||||
use crate::model::{Cartridge, Color, Sample};
|
||||
use crate::prometeu_os::NativeInterface;
|
||||
use crate::telemetry::{CertificationConfig, Certifier, TelemetryFrame};
|
||||
use crate::virtual_machine::{Value, VirtualMachine};
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
@@ -32,6 +33,12 @@ pub struct PrometeuOS {
|
||||
pub log_service: LogService,
|
||||
pub current_app_id: u32,
|
||||
pub logs_written_this_frame: HashMap<u32, u32>,
|
||||
|
||||
// Telemetria e Certificação
|
||||
pub telemetry_current: TelemetryFrame,
|
||||
pub telemetry_last: TelemetryFrame,
|
||||
pub certifier: Certifier,
|
||||
|
||||
boot_time: Instant,
|
||||
}
|
||||
|
||||
@@ -42,7 +49,7 @@ impl PrometeuOS {
|
||||
pub const MAX_LOG_LEN: usize = 256;
|
||||
pub const MAX_LOGS_PER_FRAME: u32 = 10;
|
||||
|
||||
pub fn new() -> Self {
|
||||
pub fn new(cap_config: Option<CertificationConfig>) -> Self {
|
||||
let boot_time = Instant::now();
|
||||
let mut os = Self {
|
||||
tick_index: 0,
|
||||
@@ -60,6 +67,9 @@ impl PrometeuOS {
|
||||
log_service: LogService::new(4096),
|
||||
current_app_id: 0,
|
||||
logs_written_this_frame: HashMap::new(),
|
||||
telemetry_current: TelemetryFrame::default(),
|
||||
telemetry_last: TelemetryFrame::default(),
|
||||
certifier: Certifier::new(cap_config.unwrap_or_default()),
|
||||
boot_time,
|
||||
};
|
||||
|
||||
@@ -138,6 +148,12 @@ impl PrometeuOS {
|
||||
self.logical_frame_active = true;
|
||||
self.logical_frame_remaining_cycles = Self::CYCLES_PER_LOGICAL_FRAME;
|
||||
self.begin_logical_frame(signals, hw);
|
||||
|
||||
// Início do frame: resetar acumulador da telemetria (mas manter frame_index)
|
||||
self.telemetry_current = TelemetryFrame {
|
||||
frame_index: self.logical_frame_index,
|
||||
..Default::default()
|
||||
};
|
||||
}
|
||||
|
||||
// Budget para este tick: o mínimo entre a fatia do tick e o que resta no frame lógico
|
||||
@@ -151,8 +167,23 @@ impl PrometeuOS {
|
||||
Ok(run) => {
|
||||
self.logical_frame_remaining_cycles = self.logical_frame_remaining_cycles.saturating_sub(run.cycles_used);
|
||||
|
||||
// Acumula métricas
|
||||
self.telemetry_current.cycles_used += run.cycles_used;
|
||||
self.telemetry_current.vm_steps += run.steps_executed;
|
||||
|
||||
if run.reason == crate::virtual_machine::LogicalFrameEndingReason::FrameSync {
|
||||
hw.gfx_mut().render_all();
|
||||
|
||||
// Finaliza telemetria do frame (host_cpu_time será refinado no final do tick)
|
||||
self.telemetry_current.host_cpu_time_us = start.elapsed().as_micros() as u64;
|
||||
|
||||
// Certificação (CAP)
|
||||
let ts_ms = self.boot_time.elapsed().as_millis() as u64;
|
||||
self.telemetry_current.violations = self.certifier.evaluate(&self.telemetry_current, &mut self.log_service, ts_ms) as u32;
|
||||
|
||||
// Latch: o que o overlay lê
|
||||
self.telemetry_last = self.telemetry_current;
|
||||
|
||||
self.logical_frame_index += 1;
|
||||
self.logical_frame_active = false;
|
||||
self.logical_frame_remaining_cycles = 0;
|
||||
@@ -167,6 +198,12 @@ impl PrometeuOS {
|
||||
}
|
||||
|
||||
self.last_frame_cpu_time_us = start.elapsed().as_micros() as u64;
|
||||
|
||||
// Se o frame acabou neste tick, atualizamos o tempo real final no latch
|
||||
if !self.logical_frame_active && self.telemetry_last.frame_index == self.logical_frame_index.wrapping_sub(1) {
|
||||
self.telemetry_last.host_cpu_time_us = self.last_frame_cpu_time_us;
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
@@ -277,7 +314,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_infinite_loop_budget_reset_bug() {
|
||||
let mut os = PrometeuOS::new();
|
||||
let mut os = PrometeuOS::new(None);
|
||||
let mut vm = VirtualMachine::default();
|
||||
let mut hw = Hardware::new();
|
||||
let signals = InputSignals::default();
|
||||
@@ -315,7 +352,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_budget_reset_on_frame_sync() {
|
||||
let mut os = PrometeuOS::new();
|
||||
let mut os = PrometeuOS::new(None);
|
||||
let mut vm = VirtualMachine::default();
|
||||
let mut hw = Hardware::new();
|
||||
let signals = InputSignals::default();
|
||||
@@ -360,7 +397,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_syscall_log_write_and_rate_limit() {
|
||||
let mut os = PrometeuOS::new();
|
||||
let mut os = PrometeuOS::new(None);
|
||||
let mut vm = VirtualMachine::default();
|
||||
let mut hw = Hardware::new();
|
||||
|
||||
@@ -430,6 +467,7 @@ mod tests {
|
||||
|
||||
impl NativeInterface for PrometeuOS {
|
||||
fn syscall(&mut self, id: u32, vm: &mut VirtualMachine, hw: &mut dyn HardwareBridge) -> Result<u64, String> {
|
||||
self.telemetry_current.syscalls += 1;
|
||||
match id {
|
||||
// system.has_cart() -> bool
|
||||
0x0001 => {
|
||||
|
||||
Reference in New Issue
Block a user