[PERF] Host Debug Overlay Isolation
This commit is contained in:
@@ -4,6 +4,7 @@ pub mod debugger;
|
||||
pub mod fs_backend;
|
||||
pub mod input;
|
||||
pub mod log_sink;
|
||||
pub mod overlay;
|
||||
pub mod runner;
|
||||
pub mod stats;
|
||||
pub mod utilities;
|
||||
|
||||
@@ -0,0 +1,460 @@
|
||||
use crate::stats::HostStats;
|
||||
use prometeu_firmware::Firmware;
|
||||
|
||||
const PANEL_X: usize = 6;
|
||||
const PANEL_Y: usize = 10;
|
||||
const PANEL_WIDTH: usize = 170;
|
||||
const PANEL_PADDING: usize = 8;
|
||||
const LINE_HEIGHT: usize = 12;
|
||||
const CHAR_SCALE: usize = 1;
|
||||
const BAR_WIDTH: usize = PANEL_WIDTH - (PANEL_PADDING * 2);
|
||||
const BAR_HEIGHT: usize = 6;
|
||||
|
||||
const BG: [u8; 4] = [10, 18, 32, 208];
|
||||
const BORDER: [u8; 4] = [90, 126, 170, 255];
|
||||
const TEXT: [u8; 4] = [235, 240, 255, 255];
|
||||
const DIM: [u8; 4] = [150, 168, 196, 255];
|
||||
const WARN: [u8; 4] = [255, 104, 104, 255];
|
||||
const BAR_BG: [u8; 4] = [30, 42, 61, 255];
|
||||
const BAR_FILL: [u8; 4] = [91, 184, 255, 255];
|
||||
const BAR_WARN: [u8; 4] = [255, 150, 102, 255];
|
||||
const OVERLAY_HEAP_FALLBACK_BYTES: usize = 8 * 1024 * 1024;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub(crate) struct OverlayMetric {
|
||||
label: &'static str,
|
||||
value: String,
|
||||
warn: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub(crate) struct OverlayBar {
|
||||
label: &'static str,
|
||||
value: String,
|
||||
ratio: f32,
|
||||
warn: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub(crate) struct OverlaySnapshot {
|
||||
rows: Vec<(OverlayMetric, OverlayMetric)>,
|
||||
bars: Vec<OverlayBar>,
|
||||
footer: Vec<OverlayMetric>,
|
||||
}
|
||||
|
||||
pub(crate) fn capture_snapshot(stats: &HostStats, firmware: &Firmware) -> OverlaySnapshot {
|
||||
let tel = firmware.os.atomic_telemetry.snapshot();
|
||||
let recent_logs = firmware.os.log_service.get_recent(10);
|
||||
let violations_count = recent_logs.iter().filter(|e| e.tag >= 0xCA01 && e.tag <= 0xCA07).count();
|
||||
|
||||
let mut footer = Vec::new();
|
||||
if violations_count > 0
|
||||
&& let Some(event) = recent_logs.into_iter().rev().find(|e| e.tag >= 0xCA01 && e.tag <= 0xCA07)
|
||||
{
|
||||
footer.push(OverlayMetric {
|
||||
label: "CERT",
|
||||
value: truncate_value(&event.msg, 28),
|
||||
warn: true,
|
||||
});
|
||||
}
|
||||
|
||||
if let Some(report) = firmware.os.last_crash_report.as_ref() {
|
||||
footer.push(OverlayMetric {
|
||||
label: "CRASH",
|
||||
value: truncate_value(&report.summary(), 28),
|
||||
warn: true,
|
||||
});
|
||||
}
|
||||
|
||||
let cycles_ratio = ratio(tel.cycles_used, tel.cycles_budget);
|
||||
let heap_total_bytes = firmware
|
||||
.os
|
||||
.certifier
|
||||
.config
|
||||
.max_heap_bytes
|
||||
.or(if tel.heap_max_bytes > 0 { Some(tel.heap_max_bytes) } else { None })
|
||||
.unwrap_or(OVERLAY_HEAP_FALLBACK_BYTES);
|
||||
let heap_ratio = ratio(tel.heap_used_bytes as u64, heap_total_bytes as u64);
|
||||
let gfx_ratio = ratio(tel.gfx_slots_occupied as u64, 16);
|
||||
let audio_ratio = ratio(tel.audio_slots_occupied as u64, 16);
|
||||
|
||||
OverlaySnapshot {
|
||||
rows: vec![
|
||||
(
|
||||
OverlayMetric {
|
||||
label: "FPS",
|
||||
value: format!("{:.1}", stats.current_fps),
|
||||
warn: false,
|
||||
},
|
||||
OverlayMetric {
|
||||
label: "CERT",
|
||||
value: violations_count.to_string(),
|
||||
warn: violations_count > 0,
|
||||
},
|
||||
),
|
||||
(
|
||||
OverlayMetric {
|
||||
label: "HOST",
|
||||
value: format!("{:.2}ms", stats.average_host_cpu_ms()),
|
||||
warn: false,
|
||||
},
|
||||
OverlayMetric {
|
||||
label: "STEPS",
|
||||
value: tel.vm_steps.to_string(),
|
||||
warn: false,
|
||||
},
|
||||
),
|
||||
(
|
||||
OverlayMetric {
|
||||
label: "SYSC",
|
||||
value: tel.syscalls.to_string(),
|
||||
warn: false,
|
||||
},
|
||||
OverlayMetric {
|
||||
label: "LOGS",
|
||||
value: tel.logs_count.to_string(),
|
||||
warn: false,
|
||||
},
|
||||
),
|
||||
],
|
||||
bars: vec![
|
||||
OverlayBar {
|
||||
label: "HEAP",
|
||||
value: format!(
|
||||
"{}K{}",
|
||||
tel.heap_used_bytes.div_ceil(1024),
|
||||
if heap_total_bytes > 0 {
|
||||
format!(" / {}K", heap_total_bytes.div_ceil(1024))
|
||||
} else {
|
||||
String::new()
|
||||
}
|
||||
),
|
||||
ratio: heap_ratio,
|
||||
warn: tel.heap_used_bytes >= heap_total_bytes,
|
||||
},
|
||||
OverlayBar {
|
||||
label: "BUDGET",
|
||||
value: if tel.cycles_budget > 0 {
|
||||
format!(
|
||||
"{:.1}K/{:.1}K {:.1}%",
|
||||
tel.cycles_used as f64 / 1000.0,
|
||||
tel.cycles_budget as f64 / 1000.0,
|
||||
cycles_ratio * 100.0
|
||||
)
|
||||
} else {
|
||||
"0.0K/0.0K 0.0%".to_string()
|
||||
},
|
||||
ratio: cycles_ratio,
|
||||
warn: cycles_ratio >= 0.9,
|
||||
},
|
||||
OverlayBar {
|
||||
label: "GFX",
|
||||
value: format!(
|
||||
"{} / 16 slots {}K",
|
||||
tel.gfx_slots_occupied,
|
||||
tel.gfx_used_bytes.div_ceil(1024)
|
||||
),
|
||||
ratio: gfx_ratio,
|
||||
warn: tel.gfx_inflight_bytes > 0,
|
||||
},
|
||||
OverlayBar {
|
||||
label: "AUD",
|
||||
value: format!(
|
||||
"{} / 16 slots {}K",
|
||||
tel.audio_slots_occupied,
|
||||
tel.audio_used_bytes.div_ceil(1024)
|
||||
),
|
||||
ratio: audio_ratio,
|
||||
warn: tel.audio_inflight_bytes > 0,
|
||||
},
|
||||
],
|
||||
footer,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn draw_overlay(
|
||||
frame: &mut [u8],
|
||||
frame_width: usize,
|
||||
frame_height: usize,
|
||||
snapshot: &OverlaySnapshot,
|
||||
) {
|
||||
let panel_height = frame_height.saturating_sub(PANEL_Y * 2);
|
||||
|
||||
fill_rect_alpha(frame, frame_width, frame_height, PANEL_X, PANEL_Y, PANEL_WIDTH, panel_height, BG);
|
||||
stroke_rect(frame, frame_width, frame_height, PANEL_X, PANEL_Y, PANEL_WIDTH, panel_height, BORDER);
|
||||
|
||||
let mut y = PANEL_Y + PANEL_PADDING;
|
||||
for (left, right) in &snapshot.rows {
|
||||
draw_metric_pair(frame, frame_width, frame_height, y, left, right);
|
||||
y += LINE_HEIGHT;
|
||||
}
|
||||
|
||||
for bar in &snapshot.bars {
|
||||
let color = if bar.warn { WARN } else { TEXT };
|
||||
draw_text(frame, frame_width, frame_height, PANEL_X + PANEL_PADDING, y, bar.label, DIM);
|
||||
draw_text(frame, frame_width, frame_height, PANEL_X + 48, y, &bar.value, color);
|
||||
y += LINE_HEIGHT - 2;
|
||||
|
||||
let bar_x = PANEL_X + PANEL_PADDING;
|
||||
fill_rect(frame, frame_width, frame_height, bar_x, y, BAR_WIDTH, BAR_HEIGHT, BAR_BG);
|
||||
let fill_width = ((BAR_WIDTH as f32) * bar.ratio.clamp(0.0, 1.0)).round() as usize;
|
||||
fill_rect(
|
||||
frame,
|
||||
frame_width,
|
||||
frame_height,
|
||||
bar_x,
|
||||
y,
|
||||
fill_width,
|
||||
BAR_HEIGHT,
|
||||
if bar.warn { BAR_WARN } else { BAR_FILL },
|
||||
);
|
||||
stroke_rect(frame, frame_width, frame_height, bar_x, y, BAR_WIDTH, BAR_HEIGHT, BORDER);
|
||||
y += BAR_HEIGHT + 6;
|
||||
}
|
||||
|
||||
for line in &snapshot.footer {
|
||||
let color = if line.warn { WARN } else { TEXT };
|
||||
draw_text(frame, frame_width, frame_height, PANEL_X + PANEL_PADDING, y, line.label, DIM);
|
||||
draw_text(frame, frame_width, frame_height, PANEL_X + 48, y, &line.value, color);
|
||||
y += LINE_HEIGHT;
|
||||
}
|
||||
}
|
||||
|
||||
fn draw_metric_pair(
|
||||
frame: &mut [u8],
|
||||
frame_width: usize,
|
||||
frame_height: usize,
|
||||
y: usize,
|
||||
left: &OverlayMetric,
|
||||
right: &OverlayMetric,
|
||||
) {
|
||||
let left_x = PANEL_X + PANEL_PADDING;
|
||||
let right_x = PANEL_X + 86;
|
||||
|
||||
draw_metric(frame, frame_width, frame_height, left_x, y, left);
|
||||
draw_metric(frame, frame_width, frame_height, right_x, y, right);
|
||||
}
|
||||
|
||||
fn draw_metric(
|
||||
frame: &mut [u8],
|
||||
frame_width: usize,
|
||||
frame_height: usize,
|
||||
x: usize,
|
||||
y: usize,
|
||||
metric: &OverlayMetric,
|
||||
) {
|
||||
let color = if metric.warn { WARN } else { TEXT };
|
||||
draw_text(frame, frame_width, frame_height, x, y, metric.label, DIM);
|
||||
draw_text(frame, frame_width, frame_height, x + 30, y, &metric.value, color);
|
||||
}
|
||||
|
||||
fn ratio(value: u64, total: u64) -> f32 {
|
||||
if total == 0 { 0.0 } else { (value as f32 / total as f32).clamp(0.0, 1.0) }
|
||||
}
|
||||
|
||||
fn truncate_value(value: &str, max_len: usize) -> String {
|
||||
let mut upper = value.to_ascii_uppercase();
|
||||
if upper.len() > max_len {
|
||||
upper.truncate(max_len);
|
||||
}
|
||||
upper
|
||||
}
|
||||
|
||||
fn draw_text(frame: &mut [u8], frame_width: usize, frame_height: usize, x: usize, y: usize, text: &str, color: [u8; 4]) {
|
||||
let mut cursor_x = x;
|
||||
for ch in text.chars() {
|
||||
if ch == '\n' {
|
||||
continue;
|
||||
}
|
||||
draw_char(frame, frame_width, frame_height, cursor_x, y, ch.to_ascii_uppercase(), color);
|
||||
cursor_x += 6 * CHAR_SCALE;
|
||||
}
|
||||
}
|
||||
|
||||
fn draw_char(frame: &mut [u8], frame_width: usize, frame_height: usize, x: usize, y: usize, ch: char, color: [u8; 4]) {
|
||||
let glyph = glyph_bits(ch);
|
||||
for (row, bits) in glyph.iter().enumerate() {
|
||||
for col in 0..5 {
|
||||
if bits & (1 << (4 - col)) != 0 {
|
||||
fill_rect(frame, frame_width, frame_height, x + col * CHAR_SCALE, y + row * CHAR_SCALE, CHAR_SCALE, CHAR_SCALE, color);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn fill_rect_alpha(
|
||||
frame: &mut [u8],
|
||||
frame_width: usize,
|
||||
frame_height: usize,
|
||||
x: usize,
|
||||
y: usize,
|
||||
width: usize,
|
||||
height: usize,
|
||||
color: [u8; 4],
|
||||
) {
|
||||
let max_x = (x + width).min(frame_width);
|
||||
let max_y = (y + height).min(frame_height);
|
||||
for py in y..max_y {
|
||||
for px in x..max_x {
|
||||
blend_pixel(frame, frame_width, px, py, color);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn fill_rect(
|
||||
frame: &mut [u8],
|
||||
frame_width: usize,
|
||||
frame_height: usize,
|
||||
x: usize,
|
||||
y: usize,
|
||||
width: usize,
|
||||
height: usize,
|
||||
color: [u8; 4],
|
||||
) {
|
||||
let max_x = (x + width).min(frame_width);
|
||||
let max_y = (y + height).min(frame_height);
|
||||
for py in y..max_y {
|
||||
for px in x..max_x {
|
||||
write_pixel(frame, frame_width, px, py, color);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn stroke_rect(
|
||||
frame: &mut [u8],
|
||||
frame_width: usize,
|
||||
frame_height: usize,
|
||||
x: usize,
|
||||
y: usize,
|
||||
width: usize,
|
||||
height: usize,
|
||||
color: [u8; 4],
|
||||
) {
|
||||
if width == 0 || height == 0 {
|
||||
return;
|
||||
}
|
||||
fill_rect(frame, frame_width, frame_height, x, y, width, 1, color);
|
||||
fill_rect(frame, frame_width, frame_height, x, y + height.saturating_sub(1), width, 1, color);
|
||||
fill_rect(frame, frame_width, frame_height, x, y, 1, height, color);
|
||||
fill_rect(frame, frame_width, frame_height, x + width.saturating_sub(1), y, 1, height, color);
|
||||
}
|
||||
|
||||
fn blend_pixel(frame: &mut [u8], frame_width: usize, x: usize, y: usize, color: [u8; 4]) {
|
||||
let idx = (y * frame_width + x) * 4;
|
||||
let alpha = color[3] as f32 / 255.0;
|
||||
let inv = 1.0 - alpha;
|
||||
frame[idx] = (frame[idx] as f32 * inv + color[0] as f32 * alpha).round() as u8;
|
||||
frame[idx + 1] = (frame[idx + 1] as f32 * inv + color[1] as f32 * alpha).round() as u8;
|
||||
frame[idx + 2] = (frame[idx + 2] as f32 * inv + color[2] as f32 * alpha).round() as u8;
|
||||
frame[idx + 3] = 0xFF;
|
||||
}
|
||||
|
||||
fn write_pixel(frame: &mut [u8], frame_width: usize, x: usize, y: usize, color: [u8; 4]) {
|
||||
let idx = (y * frame_width + x) * 4;
|
||||
frame[idx] = color[0];
|
||||
frame[idx + 1] = color[1];
|
||||
frame[idx + 2] = color[2];
|
||||
frame[idx + 3] = color[3];
|
||||
}
|
||||
|
||||
fn glyph_bits(ch: char) -> [u8; 7] {
|
||||
match ch {
|
||||
'A' => [0x0E, 0x11, 0x11, 0x1F, 0x11, 0x11, 0x11],
|
||||
'B' => [0x1E, 0x11, 0x11, 0x1E, 0x11, 0x11, 0x1E],
|
||||
'C' => [0x0E, 0x11, 0x10, 0x10, 0x10, 0x11, 0x0E],
|
||||
'D' => [0x1E, 0x12, 0x11, 0x11, 0x11, 0x12, 0x1E],
|
||||
'E' => [0x1F, 0x10, 0x10, 0x1E, 0x10, 0x10, 0x1F],
|
||||
'F' => [0x1F, 0x10, 0x10, 0x1E, 0x10, 0x10, 0x10],
|
||||
'G' => [0x0E, 0x11, 0x10, 0x17, 0x11, 0x11, 0x0E],
|
||||
'H' => [0x11, 0x11, 0x11, 0x1F, 0x11, 0x11, 0x11],
|
||||
'I' => [0x0E, 0x04, 0x04, 0x04, 0x04, 0x04, 0x0E],
|
||||
'J' => [0x01, 0x01, 0x01, 0x01, 0x11, 0x11, 0x0E],
|
||||
'K' => [0x11, 0x12, 0x14, 0x18, 0x14, 0x12, 0x11],
|
||||
'L' => [0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x1F],
|
||||
'M' => [0x11, 0x1B, 0x15, 0x15, 0x11, 0x11, 0x11],
|
||||
'N' => [0x11, 0x11, 0x19, 0x15, 0x13, 0x11, 0x11],
|
||||
'O' => [0x0E, 0x11, 0x11, 0x11, 0x11, 0x11, 0x0E],
|
||||
'P' => [0x1E, 0x11, 0x11, 0x1E, 0x10, 0x10, 0x10],
|
||||
'Q' => [0x0E, 0x11, 0x11, 0x11, 0x15, 0x12, 0x0D],
|
||||
'R' => [0x1E, 0x11, 0x11, 0x1E, 0x14, 0x12, 0x11],
|
||||
'S' => [0x0F, 0x10, 0x10, 0x0E, 0x01, 0x01, 0x1E],
|
||||
'T' => [0x1F, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04],
|
||||
'U' => [0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x0E],
|
||||
'V' => [0x11, 0x11, 0x11, 0x11, 0x11, 0x0A, 0x04],
|
||||
'W' => [0x11, 0x11, 0x11, 0x15, 0x15, 0x15, 0x0A],
|
||||
'X' => [0x11, 0x11, 0x0A, 0x04, 0x0A, 0x11, 0x11],
|
||||
'Y' => [0x11, 0x11, 0x0A, 0x04, 0x04, 0x04, 0x04],
|
||||
'Z' => [0x1F, 0x01, 0x02, 0x04, 0x08, 0x10, 0x1F],
|
||||
'0' => [0x0E, 0x11, 0x13, 0x15, 0x19, 0x11, 0x0E],
|
||||
'1' => [0x04, 0x0C, 0x14, 0x04, 0x04, 0x04, 0x1F],
|
||||
'2' => [0x0E, 0x11, 0x01, 0x02, 0x04, 0x08, 0x1F],
|
||||
'3' => [0x1E, 0x01, 0x01, 0x0E, 0x01, 0x01, 0x1E],
|
||||
'4' => [0x02, 0x06, 0x0A, 0x12, 0x1F, 0x02, 0x02],
|
||||
'5' => [0x1F, 0x10, 0x10, 0x1E, 0x01, 0x01, 0x1E],
|
||||
'6' => [0x0E, 0x10, 0x10, 0x1E, 0x11, 0x11, 0x0E],
|
||||
'7' => [0x1F, 0x01, 0x02, 0x04, 0x08, 0x08, 0x08],
|
||||
'8' => [0x0E, 0x11, 0x11, 0x0E, 0x11, 0x11, 0x0E],
|
||||
'9' => [0x0E, 0x11, 0x11, 0x0F, 0x01, 0x01, 0x0E],
|
||||
':' => [0x00, 0x04, 0x04, 0x00, 0x04, 0x04, 0x00],
|
||||
'.' => [0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x06],
|
||||
'/' => [0x01, 0x02, 0x02, 0x04, 0x08, 0x08, 0x10],
|
||||
'%' => [0x19, 0x19, 0x02, 0x04, 0x08, 0x13, 0x13],
|
||||
'(' => [0x02, 0x04, 0x08, 0x08, 0x08, 0x04, 0x02],
|
||||
')' => [0x08, 0x04, 0x02, 0x02, 0x02, 0x04, 0x08],
|
||||
'-' => [0x00, 0x00, 0x00, 0x1F, 0x00, 0x00, 0x00],
|
||||
'_' => [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1F],
|
||||
'+' => [0x00, 0x04, 0x04, 0x1F, 0x04, 0x04, 0x00],
|
||||
' ' => [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
|
||||
'?' => [0x0E, 0x11, 0x01, 0x02, 0x04, 0x00, 0x04],
|
||||
_ => [0x0E, 0x11, 0x01, 0x02, 0x04, 0x00, 0x04],
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn sample_snapshot() -> OverlaySnapshot {
|
||||
OverlaySnapshot {
|
||||
rows: vec![
|
||||
(
|
||||
OverlayMetric { label: "FPS", value: "60.0".to_string(), warn: false },
|
||||
OverlayMetric { label: "CERT", value: "2".to_string(), warn: true },
|
||||
),
|
||||
(
|
||||
OverlayMetric { label: "HOST", value: "1.23ms".to_string(), warn: false },
|
||||
OverlayMetric { label: "STEPS", value: "420".to_string(), warn: false },
|
||||
),
|
||||
],
|
||||
bars: vec![OverlayBar {
|
||||
label: "HEAP",
|
||||
value: "1024K / 8192K".to_string(),
|
||||
ratio: 0.125,
|
||||
warn: false,
|
||||
},
|
||||
OverlayBar {
|
||||
label: "BUDGET",
|
||||
value: "9.0K/10.0K 90.0%".to_string(),
|
||||
ratio: 0.9,
|
||||
warn: true,
|
||||
}],
|
||||
footer: vec![OverlayMetric {
|
||||
label: "CRASH",
|
||||
value: "VM PANIC".to_string(),
|
||||
warn: true,
|
||||
}],
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn draw_overlay_writes_to_host_rgba_frame() {
|
||||
let mut frame = vec![0u8; 320 * 180 * 4];
|
||||
draw_overlay(&mut frame, 320, 180, &sample_snapshot());
|
||||
assert!(frame.iter().any(|&byte| byte != 0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn truncate_value_normalizes_and_caps() {
|
||||
assert_eq!(truncate_value("panic: lowercase", 12), "PANIC: LOWER");
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ use crate::debugger::HostDebugger;
|
||||
use crate::fs_backend::HostDirBackend;
|
||||
use crate::input::HostInputHandler;
|
||||
use crate::log_sink::HostConsoleSink;
|
||||
use crate::overlay::{capture_snapshot, draw_overlay};
|
||||
use crate::stats::HostStats;
|
||||
use crate::utilities::draw_rgb565_to_rgba8;
|
||||
use pixels::wgpu::PresentMode;
|
||||
@@ -10,7 +11,6 @@ use pixels::{Pixels, PixelsBuilder, SurfaceTexture};
|
||||
use prometeu_drivers::AudioCommand;
|
||||
use prometeu_drivers::hardware::Hardware;
|
||||
use prometeu_firmware::{BootTarget, Firmware};
|
||||
use prometeu_hal::color::Color;
|
||||
use prometeu_hal::telemetry::CertificationConfig;
|
||||
use std::time::{Duration, Instant};
|
||||
use winit::application::ApplicationHandler;
|
||||
@@ -123,110 +123,6 @@ impl HostRunner {
|
||||
}
|
||||
}
|
||||
|
||||
fn display_dbg_overlay(&mut self) {
|
||||
let tel = self.firmware.os.atomic_telemetry.snapshot();
|
||||
let color_text = Color::WHITE;
|
||||
let color_bg = Color::INDIGO; // Dark blue to stand out
|
||||
let color_warn = Color::RED;
|
||||
|
||||
self.hardware.gfx.fill_rect(5, 5, 175, 130, color_bg);
|
||||
self.hardware.gfx.draw_text(
|
||||
10,
|
||||
10,
|
||||
&format!("FPS: {:.1}", self.stats.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);
|
||||
|
||||
let cycles_pct = if tel.cycles_budget > 0 {
|
||||
(tel.cycles_used as f64 / tel.cycles_budget as f64) * 100.0
|
||||
} else {
|
||||
0.0
|
||||
};
|
||||
self.hardware.gfx.draw_text(
|
||||
10,
|
||||
42,
|
||||
&format!("CYC: {}/{} ({:.1}%)", tel.cycles_used, tel.cycles_budget, cycles_pct),
|
||||
color_text,
|
||||
);
|
||||
|
||||
self.hardware.gfx.draw_text(
|
||||
10,
|
||||
50,
|
||||
&format!("GFX: {}K/16M ({}S)", tel.gfx_used_bytes / 1024, tel.gfx_slots_occupied),
|
||||
color_text,
|
||||
);
|
||||
if tel.gfx_inflight_bytes > 0 {
|
||||
self.hardware.gfx.draw_text(
|
||||
10,
|
||||
58,
|
||||
&format!("LOAD GFX: {}KB", tel.gfx_inflight_bytes / 1024),
|
||||
color_warn,
|
||||
);
|
||||
}
|
||||
|
||||
self.hardware.gfx.draw_text(
|
||||
10,
|
||||
66,
|
||||
&format!("AUD: {}K/32M ({}S)", tel.audio_used_bytes / 1024, tel.audio_slots_occupied),
|
||||
color_text,
|
||||
);
|
||||
if tel.audio_inflight_bytes > 0 {
|
||||
self.hardware.gfx.draw_text(
|
||||
10,
|
||||
74,
|
||||
&format!("LOAD AUD: {}KB", tel.audio_inflight_bytes / 1024),
|
||||
color_warn,
|
||||
);
|
||||
}
|
||||
|
||||
self.hardware.gfx.draw_text(
|
||||
10,
|
||||
82,
|
||||
&format!("RAM: {}KB", tel.heap_used_bytes / 1024),
|
||||
color_text,
|
||||
);
|
||||
self.hardware.gfx.draw_text(10, 90, &format!("LOGS: {}", tel.logs_count), color_text);
|
||||
|
||||
// Snapshot does not include violations, as they are part of certification (logical end of frame)
|
||||
// But for visual debug, we can check if there are recent CA tags in logs
|
||||
let recent_logs = self.firmware.os.log_service.get_recent(10);
|
||||
let violations_count =
|
||||
recent_logs.iter().filter(|e| e.tag >= 0xCA01 && e.tag <= 0xCA07).count();
|
||||
let cert_color = if violations_count > 0 { color_warn } else { color_text };
|
||||
self.hardware.gfx.draw_text(
|
||||
10,
|
||||
98,
|
||||
&format!("CERT RECENT: {}", violations_count),
|
||||
cert_color,
|
||||
);
|
||||
|
||||
if violations_count > 0
|
||||
&& let Some(event) =
|
||||
recent_logs.into_iter().rev().find(|e| e.tag >= 0xCA01 && e.tag <= 0xCA07)
|
||||
{
|
||||
let mut msg = event.msg.clone();
|
||||
if msg.len() > 30 {
|
||||
msg.truncate(30);
|
||||
}
|
||||
self.hardware.gfx.draw_text(10, 106, &msg, color_warn);
|
||||
}
|
||||
|
||||
if let Some(report) = self.firmware.os.last_crash_report.as_ref() {
|
||||
let mut msg = report.summary();
|
||||
if msg.len() > 30 {
|
||||
msg.truncate(30);
|
||||
}
|
||||
self.hardware.gfx.draw_text(10, 114, &msg, color_warn);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ApplicationHandler for HostRunner {
|
||||
@@ -280,6 +176,9 @@ impl ApplicationHandler for HostRunner {
|
||||
}
|
||||
|
||||
WindowEvent::RedrawRequested => {
|
||||
let overlay_snapshot =
|
||||
self.overlay_enabled.then(|| capture_snapshot(&self.stats, &self.firmware));
|
||||
|
||||
// Get Pixels directly from the field (not via helper that gets the entire &mut self)
|
||||
let pixels = self.pixels.as_mut().expect("pixels not initialized");
|
||||
|
||||
@@ -291,6 +190,10 @@ impl ApplicationHandler for HostRunner {
|
||||
let src = self.hardware.gfx.front_buffer();
|
||||
|
||||
draw_rgb565_to_rgba8(src, frame);
|
||||
|
||||
if let Some(snapshot) = overlay_snapshot.as_ref() {
|
||||
draw_overlay(frame, Hardware::W, Hardware::H, snapshot);
|
||||
}
|
||||
} // <- frame borrow ends here
|
||||
|
||||
if pixels.render().is_err() {
|
||||
@@ -358,6 +261,7 @@ impl ApplicationHandler for HostRunner {
|
||||
// Unless the debugger is waiting for a 'start' command, advance the system.
|
||||
if !self.debugger.waiting_for_start {
|
||||
self.firmware.tick(&self.input.signals, &mut self.hardware);
|
||||
self.stats.record_host_cpu_time(self.firmware.os.last_frame_cpu_time_us);
|
||||
}
|
||||
|
||||
// Sync pause state with audio.
|
||||
@@ -393,15 +297,8 @@ impl ApplicationHandler for HostRunner {
|
||||
};
|
||||
self.log_sink.process_events(new_events);
|
||||
|
||||
// 5. Rendering the Telemetry Overlay (if enabled).
|
||||
if self.overlay_enabled {
|
||||
// We temporarily swap buffers to draw over the current image.
|
||||
self.hardware.gfx.present();
|
||||
self.display_dbg_overlay();
|
||||
self.hardware.gfx.present();
|
||||
}
|
||||
|
||||
// Finally, request a window redraw to present the new pixels.
|
||||
// 5. Request redraw so the host surface can present the latest machine frame
|
||||
// and, when enabled, compose the overlay in the host-only RGBA surface.
|
||||
self.request_redraw();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,9 @@ pub struct HostStats {
|
||||
pub current_fps: f64,
|
||||
pub audio_load_accum_us: u64,
|
||||
pub audio_load_samples: u64,
|
||||
recent_host_cpu_us: [u64; 5],
|
||||
recent_host_cpu_count: usize,
|
||||
recent_host_cpu_cursor: usize,
|
||||
}
|
||||
|
||||
impl Default for HostStats {
|
||||
@@ -25,6 +28,9 @@ impl HostStats {
|
||||
current_fps: 0.0,
|
||||
audio_load_accum_us: 0,
|
||||
audio_load_samples: 0,
|
||||
recent_host_cpu_us: [0; 5],
|
||||
recent_host_cpu_count: 0,
|
||||
recent_host_cpu_cursor: 0,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,6 +43,21 @@ impl HostStats {
|
||||
self.audio_load_samples += 1;
|
||||
}
|
||||
|
||||
pub fn record_host_cpu_time(&mut self, us: u64) {
|
||||
self.recent_host_cpu_us[self.recent_host_cpu_cursor] = us;
|
||||
self.recent_host_cpu_cursor = (self.recent_host_cpu_cursor + 1) % self.recent_host_cpu_us.len();
|
||||
self.recent_host_cpu_count = self.recent_host_cpu_count.saturating_add(1).min(self.recent_host_cpu_us.len());
|
||||
}
|
||||
|
||||
pub fn average_host_cpu_ms(&self) -> f64 {
|
||||
if self.recent_host_cpu_count == 0 {
|
||||
0.0
|
||||
} else {
|
||||
let sum: u64 = self.recent_host_cpu_us.iter().take(self.recent_host_cpu_count).sum();
|
||||
(sum as f64 / self.recent_host_cpu_count as f64) / 1000.0
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update(
|
||||
&mut self,
|
||||
now: Instant,
|
||||
@@ -51,8 +72,7 @@ impl HostStats {
|
||||
if let Some(window) = window {
|
||||
// Fixed comparison always against 60Hz, keep even when doing CPU stress tests
|
||||
let frame_budget_us = 16666.0;
|
||||
let cpu_load_core =
|
||||
(firmware.os.last_frame_cpu_time_us as f64 / frame_budget_us) * 100.0;
|
||||
let cpu_load_core = (self.average_host_cpu_ms() * 1000.0 / frame_budget_us) * 100.0;
|
||||
|
||||
let cpu_load_audio = if self.audio_load_samples > 0 {
|
||||
(self.audio_load_accum_us as f64 / stats_elapsed.as_micros() as f64) * 100.0
|
||||
|
||||
Reference in New Issue
Block a user