[PERF] Host Debug Overlay Isolation

This commit is contained in:
2026-04-10 19:16:44 +01:00
parent 58a5f9a3a6
commit 4cdfaefda7
17 changed files with 490 additions and 402 deletions
+27 -64
View File
@@ -2,8 +2,8 @@
use crate::memory_banks::{GlyphBankPoolInstaller, SoundBankPoolInstaller};
use prometeu_hal::AssetBridge;
use prometeu_hal::asset::{
AssetCodec, AssetEntry, AssetId, AssetLoadError, AssetOpStatus, BankStats, BankType, HandleId,
LoadStatus, PreloadEntry, SlotRef, SlotStats,
AssetCodec, AssetEntry, AssetId, AssetLoadError, AssetOpStatus, BankTelemetry, BankType,
HandleId, LoadStatus, PreloadEntry, SlotRef, SlotStats,
};
use prometeu_hal::cartridge::AssetsPayloadSource;
use prometeu_hal::color::Color;
@@ -161,11 +161,6 @@ pub struct AssetManager {
/// Residency policy for sound banks.
sound_policy: BankPolicy<SoundBank>,
/// Count of occupied slots for GFX.
gfx_slots_occupied: AtomicUsize,
/// Count of occupied slots for sounds.
sound_slots_occupied: AtomicUsize,
// Commits that are ready to be applied at the next frame boundary.
pending_commits: Mutex<Vec<HandleId>>,
}
@@ -206,8 +201,8 @@ impl AssetBridge for AssetManager {
fn apply_commits(&self) {
self.apply_commits()
}
fn bank_info(&self, kind: BankType) -> BankStats {
self.bank_info(kind)
fn bank_telemetry(&self) -> Vec<BankTelemetry> {
self.bank_telemetry()
}
fn slot_info(&self, slot: SlotRef) -> SlotStats {
self.slot_info(slot)
@@ -302,8 +297,6 @@ impl AssetManager {
sound_slots: Arc::new(RwLock::new(std::array::from_fn(|_| None))),
gfx_policy: BankPolicy::new(),
sound_policy: BankPolicy::new(),
gfx_slots_occupied: AtomicUsize::new(0),
sound_slots_occupied: AtomicUsize::new(0),
handles: Arc::new(RwLock::new(HashMap::new())),
next_handle_id: Mutex::new(1),
assets_data: Arc::new(RwLock::new(assets_data)),
@@ -732,9 +725,6 @@ impl AssetManager {
self.gfx_installer.install_glyph_bank(h.slot.index, bank);
let mut slots = self.gfx_slots.write().unwrap();
if h.slot.index < slots.len() {
if slots[h.slot.index].is_none() {
self.gfx_slots_occupied.fetch_add(1, Ordering::Relaxed);
}
slots[h.slot.index] = Some(h._asset_id);
}
h.status = LoadStatus::COMMITTED;
@@ -745,9 +735,6 @@ impl AssetManager {
self.sound_installer.install_sound_bank(h.slot.index, bank);
let mut slots = self.sound_slots.write().unwrap();
if h.slot.index < slots.len() {
if slots[h.slot.index].is_none() {
self.sound_slots_occupied.fetch_add(1, Ordering::Relaxed);
}
slots[h.slot.index] = Some(h._asset_id);
}
h.status = LoadStatus::COMMITTED;
@@ -759,37 +746,21 @@ impl AssetManager {
}
}
pub fn bank_info(&self, kind: BankType) -> BankStats {
match kind {
BankType::GLYPH => {
let used_bytes = self.gfx_policy.used_bytes.load(Ordering::Relaxed);
let inflight_bytes = self.gfx_policy.inflight_bytes.load(Ordering::Relaxed);
let slots_occupied = self.gfx_slots_occupied.load(Ordering::Relaxed);
pub fn bank_telemetry(&self) -> Vec<BankTelemetry> {
vec![self.bank_telemetry_for(BankType::GLYPH), self.bank_telemetry_for(BankType::SOUNDS)]
}
BankStats {
total_bytes: 16 * 1024 * 1024,
used_bytes,
free_bytes: (16usize * 1024 * 1024).saturating_sub(used_bytes),
inflight_bytes,
slot_count: 16,
slots_occupied,
}
fn bank_telemetry_for(&self, kind: BankType) -> BankTelemetry {
let used_slots = match kind {
BankType::GLYPH => {
self.gfx_slots.read().unwrap().iter().filter(|slot| slot.is_some()).count()
}
BankType::SOUNDS => {
let used_bytes = self.sound_policy.used_bytes.load(Ordering::Relaxed);
let inflight_bytes = self.sound_policy.inflight_bytes.load(Ordering::Relaxed);
let slots_occupied = self.sound_slots_occupied.load(Ordering::Relaxed);
BankStats {
total_bytes: 32 * 1024 * 1024,
used_bytes,
free_bytes: (32usize * 1024 * 1024).saturating_sub(used_bytes),
inflight_bytes,
slot_count: 16,
slots_occupied,
}
self.sound_slots.read().unwrap().iter().filter(|slot| slot.is_some()).count()
}
}
};
BankTelemetry { bank_type: kind, used_slots, total_slots: 16 }
}
pub fn slot_info(&self, slot: SlotRef) -> SlotStats {
@@ -842,8 +813,6 @@ impl AssetManager {
pub fn shutdown(&self) {
self.gfx_policy.clear();
self.sound_policy.clear();
self.gfx_slots_occupied.store(0, Ordering::Relaxed);
self.sound_slots_occupied.store(0, Ordering::Relaxed);
self.handles.write().unwrap().clear();
self.pending_commits.lock().unwrap().clear();
self.gfx_slots.write().unwrap().fill(None);
@@ -1196,7 +1165,6 @@ mod tests {
let width = 16;
let height = 16;
let decoded_bytes = expected_glyph_decoded_size(width, height);
let data = test_glyph_asset_data();
let am = AssetManager::new(
@@ -1207,10 +1175,10 @@ mod tests {
);
// Initially zero
let info = am.bank_info(BankType::GLYPH);
assert_eq!(info.used_bytes, 0);
assert_eq!(info.inflight_bytes, 0);
assert_eq!(info.slots_occupied, 0);
let info = am.bank_telemetry();
assert_eq!(info[0].bank_type, BankType::GLYPH);
assert_eq!(info[0].used_slots, 0);
assert_eq!(info[0].total_slots, 16);
// Loading
let handle = am.load(0, 0).expect("load must allocate handle");
@@ -1221,26 +1189,21 @@ mod tests {
thread::sleep(std::time::Duration::from_millis(10));
}
let info = am.bank_info(BankType::GLYPH);
// Note: put_resident happens in worker thread, then stage happens.
assert_eq!(info.used_bytes, decoded_bytes);
assert_eq!(info.inflight_bytes, decoded_bytes);
assert_eq!(info.slots_occupied, 0);
let info = am.bank_telemetry();
assert_eq!(info[0].used_slots, 0);
// Commit
am.commit(handle);
am.apply_commits();
let info = am.bank_info(BankType::GLYPH);
assert_eq!(info.used_bytes, decoded_bytes);
assert_eq!(info.inflight_bytes, 0);
assert_eq!(info.slots_occupied, 1);
let info = am.bank_telemetry();
assert_eq!(info[0].used_slots, 1);
assert_eq!(info[1].bank_type, BankType::SOUNDS);
assert_eq!(info[1].used_slots, 0);
// Shutdown resets
am.shutdown();
let info = am.bank_info(BankType::GLYPH);
assert_eq!(info.used_bytes, 0);
assert_eq!(info.inflight_bytes, 0);
assert_eq!(info.slots_occupied, 0);
let info = am.bank_telemetry();
assert_eq!(info[0].used_slots, 0);
}
}
+4 -7
View File
@@ -105,13 +105,10 @@ pub enum AssetOpStatus {
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BankStats {
pub total_bytes: usize,
pub used_bytes: usize,
pub free_bytes: usize,
pub inflight_bytes: usize,
pub slot_count: usize,
pub slots_occupied: usize,
pub struct BankTelemetry {
pub bank_type: BankType,
pub used_slots: usize,
pub total_slots: usize,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
@@ -1,5 +1,5 @@
use crate::asset::{
AssetEntry, AssetId, AssetLoadError, AssetOpStatus, BankStats, BankType, HandleId, LoadStatus,
AssetEntry, AssetId, AssetLoadError, AssetOpStatus, BankTelemetry, HandleId, LoadStatus,
PreloadEntry, SlotRef, SlotStats,
};
use crate::cartridge::AssetsPayloadSource;
@@ -16,7 +16,7 @@ pub trait AssetBridge {
fn commit(&self, handle: HandleId) -> AssetOpStatus;
fn cancel(&self, handle: HandleId) -> AssetOpStatus;
fn apply_commits(&self);
fn bank_info(&self, kind: BankType) -> BankStats;
fn bank_telemetry(&self) -> Vec<BankTelemetry>;
fn slot_info(&self, slot: SlotRef) -> SlotStats;
fn shutdown(&self);
}
@@ -64,12 +64,10 @@ pub enum DebugEvent {
cycles_budget: u64,
host_cpu_time_us: u64,
violations: u32,
gfx_used_bytes: usize,
gfx_inflight_bytes: usize,
gfx_slots_occupied: u32,
audio_used_bytes: usize,
audio_inflight_bytes: usize,
audio_slots_occupied: u32,
glyph_slots_used: u32,
glyph_slots_total: u32,
sound_slots_used: u32,
sound_slots_total: u32,
},
#[serde(rename = "fault")]
Fault {
@@ -97,12 +95,10 @@ mod tests {
cycles_budget: 10000,
host_cpu_time_us: 1200,
violations: 0,
gfx_used_bytes: 1024,
gfx_inflight_bytes: 0,
gfx_slots_occupied: 1,
audio_used_bytes: 2048,
audio_inflight_bytes: 0,
audio_slots_occupied: 2,
glyph_slots_used: 1,
glyph_slots_total: 16,
sound_slots_used: 2,
sound_slots_total: 16,
};
let json = serde_json::to_string(&event).unwrap();
+33 -45
View File
@@ -13,15 +13,11 @@ pub struct TelemetryFrame {
pub completed_logical_frames: u32,
pub violations: u32,
// GFX Banks
pub gfx_used_bytes: usize,
pub gfx_inflight_bytes: usize,
pub gfx_slots_occupied: u32,
// Audio Banks
pub audio_used_bytes: usize,
pub audio_inflight_bytes: usize,
pub audio_slots_occupied: u32,
// Bank telemetry
pub glyph_slots_used: u32,
pub glyph_slots_total: u32,
pub sound_slots_used: u32,
pub sound_slots_total: u32,
// RAM (Heap)
pub heap_used_bytes: usize,
@@ -44,15 +40,11 @@ pub struct AtomicTelemetry {
pub completed_logical_frames: AtomicU32,
pub violations: AtomicU32,
// GFX Banks
pub gfx_used_bytes: AtomicUsize,
pub gfx_inflight_bytes: AtomicUsize,
pub gfx_slots_occupied: AtomicU32,
// Audio Banks
pub audio_used_bytes: AtomicUsize,
pub audio_inflight_bytes: AtomicUsize,
pub audio_slots_occupied: AtomicU32,
// Bank telemetry
pub glyph_slots_used: AtomicU32,
pub glyph_slots_total: AtomicU32,
pub sound_slots_used: AtomicU32,
pub sound_slots_total: AtomicU32,
// RAM (Heap)
pub heap_used_bytes: AtomicUsize,
@@ -79,12 +71,10 @@ impl AtomicTelemetry {
host_cpu_time_us: self.host_cpu_time_us.load(Ordering::Relaxed),
completed_logical_frames: self.completed_logical_frames.load(Ordering::Relaxed),
violations: self.violations.load(Ordering::Relaxed),
gfx_used_bytes: self.gfx_used_bytes.load(Ordering::Relaxed),
gfx_inflight_bytes: self.gfx_inflight_bytes.load(Ordering::Relaxed),
gfx_slots_occupied: self.gfx_slots_occupied.load(Ordering::Relaxed),
audio_used_bytes: self.audio_used_bytes.load(Ordering::Relaxed),
audio_inflight_bytes: self.audio_inflight_bytes.load(Ordering::Relaxed),
audio_slots_occupied: self.audio_slots_occupied.load(Ordering::Relaxed),
glyph_slots_used: self.glyph_slots_used.load(Ordering::Relaxed),
glyph_slots_total: self.glyph_slots_total.load(Ordering::Relaxed),
sound_slots_used: self.sound_slots_used.load(Ordering::Relaxed),
sound_slots_total: self.sound_slots_total.load(Ordering::Relaxed),
heap_used_bytes: self.heap_used_bytes.load(Ordering::Relaxed),
heap_max_bytes: self.heap_max_bytes.load(Ordering::Relaxed),
logs_count: self.logs_count.load(Ordering::Relaxed),
@@ -99,12 +89,10 @@ impl AtomicTelemetry {
self.host_cpu_time_us.store(0, Ordering::Relaxed);
self.completed_logical_frames.store(0, Ordering::Relaxed);
self.violations.store(0, Ordering::Relaxed);
self.gfx_used_bytes.store(0, Ordering::Relaxed);
self.gfx_inflight_bytes.store(0, Ordering::Relaxed);
self.gfx_slots_occupied.store(0, Ordering::Relaxed);
self.audio_used_bytes.store(0, Ordering::Relaxed);
self.audio_inflight_bytes.store(0, Ordering::Relaxed);
self.audio_slots_occupied.store(0, Ordering::Relaxed);
self.glyph_slots_used.store(0, Ordering::Relaxed);
self.glyph_slots_total.store(0, Ordering::Relaxed);
self.sound_slots_used.store(0, Ordering::Relaxed);
self.sound_slots_total.store(0, Ordering::Relaxed);
self.heap_used_bytes.store(0, Ordering::Relaxed);
self.vm_steps.store(0, Ordering::Relaxed);
self.logs_count.store(0, Ordering::Relaxed);
@@ -118,8 +106,8 @@ pub struct CertificationConfig {
pub cycles_budget_per_frame: Option<u64>,
pub max_syscalls_per_frame: Option<u32>,
pub max_host_cpu_us_per_frame: Option<u64>,
pub max_gfx_bytes: Option<usize>,
pub max_audio_bytes: Option<usize>,
pub max_glyph_slots_used: Option<u32>,
pub max_sound_slots_used: Option<u32>,
pub max_heap_bytes: Option<usize>,
pub max_logs_per_frame: Option<u32>,
}
@@ -199,9 +187,9 @@ impl Certifier {
violations += 1;
}
// 4. GFX Memory
if let Some(limit) = self.config.max_gfx_bytes
&& telemetry.gfx_used_bytes > limit
// 4. GLYPH bank slots
if let Some(limit) = self.config.max_glyph_slots_used
&& telemetry.glyph_slots_used > limit
{
log_service.log(
ts_ms,
@@ -210,16 +198,16 @@ impl Certifier {
LogSource::Pos,
0xCA04,
format!(
"Cert: GFX bank exceeded memory limit ({} > {})",
telemetry.gfx_used_bytes, limit
"Cert: GLYPH bank exceeded slot limit ({} > {})",
telemetry.glyph_slots_used, limit
),
);
violations += 1;
}
// 5. Audio Memory
if let Some(limit) = self.config.max_audio_bytes
&& telemetry.audio_used_bytes > limit
// 5. SOUNDS bank slots
if let Some(limit) = self.config.max_sound_slots_used
&& telemetry.sound_slots_used > limit
{
log_service.log(
ts_ms,
@@ -228,8 +216,8 @@ impl Certifier {
LogSource::Pos,
0xCA05,
format!(
"Cert: Audio bank exceeded memory limit ({} > {})",
telemetry.audio_used_bytes, limit
"Cert: SOUNDS bank exceeded slot limit ({} > {})",
telemetry.sound_slots_used, limit
),
);
violations += 1;
@@ -285,7 +273,7 @@ mod tests {
cycles_budget_per_frame: Some(100),
max_syscalls_per_frame: Some(5),
max_host_cpu_us_per_frame: Some(1000),
max_gfx_bytes: Some(1024),
max_glyph_slots_used: Some(1),
..Default::default()
};
let cert = Certifier::new(config);
@@ -294,7 +282,7 @@ mod tests {
tel.cycles_used = 150;
tel.syscalls = 10;
tel.host_cpu_time_us = 500;
tel.gfx_used_bytes = 2048;
tel.glyph_slots_used = 2;
let violations = cert.evaluate(&tel, &mut ls, 1000);
assert_eq!(violations, 3);
@@ -303,7 +291,7 @@ mod tests {
assert_eq!(logs.len(), 3);
assert!(logs[0].msg.contains("cycles_used"));
assert!(logs[1].msg.contains("syscalls"));
assert!(logs[2].msg.contains("GFX bank"));
assert!(logs[2].msg.contains("GLYPH bank"));
}
#[test]
@@ -486,8 +486,17 @@ impl NativeInterface for VirtualMachineRuntime {
1 => BankType::SOUNDS,
_ => return Err(VmFault::Trap(TRAP_TYPE, "Invalid asset type".to_string())),
};
let json =
serde_json::to_string(&hw.assets().bank_info(asset_type)).unwrap_or_default();
let telemetry = hw
.assets()
.bank_telemetry()
.into_iter()
.find(|entry| entry.bank_type == asset_type)
.unwrap_or(prometeu_hal::asset::BankTelemetry {
bank_type: asset_type,
used_slots: 0,
total_slots: 0,
});
let json = serde_json::to_string(&telemetry).unwrap_or_default();
ret.push_string(json);
Ok(())
}
@@ -1,12 +1,26 @@
use super::*;
use crate::CrashReport;
use prometeu_hal::asset::BankType;
use prometeu_hal::asset::{BankTelemetry, BankType};
use prometeu_hal::log::{LogLevel, LogSource};
use prometeu_hal::{HardwareBridge, HostContext, InputSignals};
use prometeu_vm::LogicalFrameEndingReason;
use std::sync::atomic::Ordering;
impl VirtualMachineRuntime {
fn bank_telemetry_summary(hw: &dyn HardwareBridge) -> (BankTelemetry, BankTelemetry) {
let telemetry = hw.assets().bank_telemetry();
let glyph =
telemetry.iter().find(|entry| entry.bank_type == BankType::GLYPH).cloned().unwrap_or(
BankTelemetry { bank_type: BankType::GLYPH, used_slots: 0, total_slots: 0 },
);
let sounds =
telemetry.iter().find(|entry| entry.bank_type == BankType::SOUNDS).cloned().unwrap_or(
BankTelemetry { bank_type: BankType::SOUNDS, used_slots: 0, total_slots: 0 },
);
(glyph, sounds)
}
pub fn debug_step_instruction(
&mut self,
vm: &mut VirtualMachine,
@@ -134,27 +148,19 @@ impl VirtualMachineRuntime {
hw.gfx_mut().render_all();
// 1. Snapshot full telemetry at logical frame end
let gfx_stats = hw.assets().bank_info(BankType::GLYPH);
let (glyph_bank, sound_bank) = Self::bank_telemetry_summary(hw);
self.atomic_telemetry
.gfx_used_bytes
.store(gfx_stats.used_bytes, Ordering::Relaxed);
.glyph_slots_used
.store(glyph_bank.used_slots as u32, Ordering::Relaxed);
self.atomic_telemetry
.gfx_inflight_bytes
.store(gfx_stats.inflight_bytes, Ordering::Relaxed);
.glyph_slots_total
.store(glyph_bank.total_slots as u32, Ordering::Relaxed);
self.atomic_telemetry
.gfx_slots_occupied
.store(gfx_stats.slots_occupied as u32, Ordering::Relaxed);
let audio_stats = hw.assets().bank_info(BankType::SOUNDS);
.sound_slots_used
.store(sound_bank.used_slots as u32, Ordering::Relaxed);
self.atomic_telemetry
.audio_used_bytes
.store(audio_stats.used_bytes, Ordering::Relaxed);
self.atomic_telemetry
.audio_inflight_bytes
.store(audio_stats.inflight_bytes, Ordering::Relaxed);
self.atomic_telemetry
.audio_slots_occupied
.store(audio_stats.slots_occupied as u32, Ordering::Relaxed);
.sound_slots_total
.store(sound_bank.total_slots as u32, Ordering::Relaxed);
self.atomic_telemetry
.heap_used_bytes
@@ -165,7 +171,9 @@ impl VirtualMachineRuntime {
let current_frame_logs =
self.atomic_telemetry.current_logs_count.load(Ordering::Relaxed);
self.atomic_telemetry.logs_count.store(current_frame_logs, Ordering::Relaxed);
self.atomic_telemetry
.logs_count
.store(current_frame_logs, Ordering::Relaxed);
let ts_ms = self.boot_time.elapsed().as_millis() as u64;
let telemetry_snapshot = self.atomic_telemetry.snapshot();
@@ -210,23 +218,19 @@ impl VirtualMachineRuntime {
// 2. High-frequency telemetry update (only if inspection is active)
if self.inspection_active {
let gfx_stats = hw.assets().bank_info(BankType::GLYPH);
self.atomic_telemetry.gfx_used_bytes.store(gfx_stats.used_bytes, Ordering::Relaxed);
let (glyph_bank, sound_bank) = Self::bank_telemetry_summary(hw);
self.atomic_telemetry
.gfx_inflight_bytes
.store(gfx_stats.inflight_bytes, Ordering::Relaxed);
.glyph_slots_used
.store(glyph_bank.used_slots as u32, Ordering::Relaxed);
self.atomic_telemetry
.gfx_slots_occupied
.store(gfx_stats.slots_occupied as u32, Ordering::Relaxed);
let audio_stats = hw.assets().bank_info(BankType::SOUNDS);
self.atomic_telemetry.audio_used_bytes.store(audio_stats.used_bytes, Ordering::Relaxed);
.glyph_slots_total
.store(glyph_bank.total_slots as u32, Ordering::Relaxed);
self.atomic_telemetry
.audio_inflight_bytes
.store(audio_stats.inflight_bytes, Ordering::Relaxed);
.sound_slots_used
.store(sound_bank.used_slots as u32, Ordering::Relaxed);
self.atomic_telemetry
.audio_slots_occupied
.store(audio_stats.slots_occupied as u32, Ordering::Relaxed);
.sound_slots_total
.store(sound_bank.total_slots as u32, Ordering::Relaxed);
self.atomic_telemetry
.heap_used_bytes
@@ -272,6 +272,16 @@ impl HostDebugger {
tel.host_cpu_time_us,
cert_config.max_host_cpu_us_per_frame.unwrap_or(0),
),
0xCA04 => (
"max_glyph_slots_used".to_string(),
tel.glyph_slots_used as u64,
cert_config.max_glyph_slots_used.unwrap_or(0) as u64,
),
0xCA05 => (
"max_sound_slots_used".to_string(),
tel.sound_slots_used as u64,
cert_config.max_sound_slots_used.unwrap_or(0) as u64,
),
_ => ("unknown".to_string(), 0, 0),
};
@@ -302,12 +312,10 @@ impl HostDebugger {
cycles_budget: tel.cycles_budget,
host_cpu_time_us: tel.host_cpu_time_us,
violations: tel.violations,
gfx_used_bytes: tel.gfx_used_bytes,
gfx_inflight_bytes: tel.gfx_inflight_bytes,
gfx_slots_occupied: tel.gfx_slots_occupied,
audio_used_bytes: tel.audio_used_bytes,
audio_inflight_bytes: tel.audio_inflight_bytes,
audio_slots_occupied: tel.audio_slots_occupied,
glyph_slots_used: tel.glyph_slots_used,
glyph_slots_total: tel.glyph_slots_total,
sound_slots_used: tel.sound_slots_used,
sound_slots_total: tel.sound_slots_total,
});
self.last_telemetry_frame = current_frame;
}
@@ -42,14 +42,30 @@ pub(crate) struct OverlaySnapshot {
footer: Vec<OverlayMetric>,
}
#[derive(Clone, Copy)]
struct Rect {
x: usize,
y: usize,
width: usize,
height: usize,
}
struct FrameCanvas<'a> {
frame: &'a mut [u8],
width: usize,
height: usize,
}
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 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)
&& let Some(event) =
recent_logs.into_iter().rev().find(|e| e.tag >= 0xCA01 && e.tag <= 0xCA07)
{
footer.push(OverlayMetric {
label: "CERT",
@@ -75,8 +91,8 @@ pub(crate) fn capture_snapshot(stats: &HostStats, firmware: &Firmware) -> Overla
.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);
let glyph_ratio = ratio(tel.glyph_slots_used as u64, tel.glyph_slots_total as u64);
let sound_ratio = ratio(tel.sound_slots_used as u64, tel.sound_slots_total as u64);
OverlaySnapshot {
rows: vec![
@@ -98,23 +114,11 @@ pub(crate) fn capture_snapshot(stats: &HostStats, firmware: &Firmware) -> Overla
value: format!("{:.2}ms", stats.average_host_cpu_ms()),
warn: false,
},
OverlayMetric {
label: "STEPS",
value: tel.vm_steps.to_string(),
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,
},
OverlayMetric { label: "SYSC", value: tel.syscalls.to_string(), warn: false },
OverlayMetric { label: "LOGS", value: tel.logs_count.to_string(), warn: false },
),
],
bars: vec![
@@ -136,36 +140,27 @@ pub(crate) fn capture_snapshot(stats: &HostStats, firmware: &Firmware) -> Overla
label: "BUDGET",
value: if tel.cycles_budget > 0 {
format!(
"{:.1}K/{:.1}K {:.1}%",
"{:.1}K/{:.1}K",
tel.cycles_used as f64 / 1000.0,
tel.cycles_budget as f64 / 1000.0,
cycles_ratio * 100.0
tel.cycles_budget as f64 / 1000.0
)
} else {
"0.0K/0.0K 0.0%".to_string()
"0.0K/0.0K".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,
label: "GLYPH",
value: format!("{} / {} slots", tel.glyph_slots_used, tel.glyph_slots_total),
ratio: glyph_ratio,
warn: tel.glyph_slots_total > 0 && tel.glyph_slots_used >= tel.glyph_slots_total,
},
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,
label: "SOUNDS",
value: format!("{} / {} slots", tel.sound_slots_used, tel.sound_slots_total),
ratio: sound_ratio,
warn: tel.sound_slots_total > 0 && tel.sound_slots_used >= tel.sound_slots_total,
},
],
footer,
@@ -178,44 +173,58 @@ pub(crate) fn draw_overlay(
frame_height: usize,
snapshot: &OverlaySnapshot,
) {
let mut canvas = FrameCanvas { frame, width: frame_width, height: frame_height };
let panel_height = frame_height.saturating_sub(PANEL_Y * 2);
let panel_rect = Rect { x: PANEL_X, y: PANEL_Y, width: PANEL_WIDTH, height: panel_height };
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);
fill_rect_alpha(&mut canvas, panel_rect, BG);
stroke_rect(&mut canvas, panel_rect, 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);
draw_metric_pair(canvas.frame, canvas.width, canvas.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);
draw_text(
canvas.frame,
canvas.width,
canvas.height,
PANEL_X + PANEL_PADDING,
y,
bar.label,
DIM,
);
draw_text(canvas.frame, canvas.width, canvas.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 bar_rect = Rect { x: bar_x, y, width: BAR_WIDTH, height: BAR_HEIGHT };
fill_rect(&mut canvas, bar_rect, 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,
&mut canvas,
Rect { x: bar_x, y, width: fill_width, height: 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);
stroke_rect(&mut canvas, bar_rect, 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);
draw_text(
canvas.frame,
canvas.width,
canvas.height,
PANEL_X + PANEL_PADDING,
y,
line.label,
DIM,
);
draw_text(canvas.frame, canvas.width, canvas.height, PANEL_X + 48, y, &line.value, color);
y += LINE_HEIGHT;
}
}
@@ -260,7 +269,15 @@ fn truncate_value(value: &str, max_len: usize) -> String {
upper
}
fn draw_text(frame: &mut [u8], frame_width: usize, frame_height: usize, x: usize, y: usize, text: &str, color: [u8; 4]) {
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' {
@@ -271,72 +288,71 @@ fn draw_text(frame: &mut [u8], frame_width: usize, frame_height: usize, x: usize
}
}
fn draw_char(frame: &mut [u8], frame_width: usize, frame_height: usize, x: usize, y: usize, ch: char, color: [u8; 4]) {
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);
let mut canvas = FrameCanvas { frame, width: frame_width, height: frame_height };
fill_rect(
&mut canvas,
Rect {
x: x + col * CHAR_SCALE,
y: y + row * CHAR_SCALE,
width: CHAR_SCALE,
height: 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_alpha(canvas: &mut FrameCanvas<'_>, rect: Rect, color: [u8; 4]) {
let max_x = (rect.x + rect.width).min(canvas.width);
let max_y = (rect.y + rect.height).min(canvas.height);
for py in rect.y..max_y {
for px in rect.x..max_x {
blend_pixel(canvas.frame, canvas.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 fill_rect(canvas: &mut FrameCanvas<'_>, rect: Rect, color: [u8; 4]) {
let max_x = (rect.x + rect.width).min(canvas.width);
let max_y = (rect.y + rect.height).min(canvas.height);
for py in rect.y..max_y {
for px in rect.x..max_x {
write_pixel(canvas.frame, canvas.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 {
fn stroke_rect(canvas: &mut FrameCanvas<'_>, rect: Rect, color: [u8; 4]) {
if rect.width == 0 || rect.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);
fill_rect(canvas, Rect { x: rect.x, y: rect.y, width: rect.width, height: 1 }, color);
fill_rect(
canvas,
Rect { x: rect.x, y: rect.y + rect.height.saturating_sub(1), width: rect.width, height: 1 },
color,
);
fill_rect(canvas, Rect { x: rect.x, y: rect.y, width: 1, height: rect.height }, color);
fill_rect(
canvas,
Rect { x: rect.x + rect.width.saturating_sub(1), y: rect.y, width: 1, height: rect.height },
color,
);
}
fn blend_pixel(frame: &mut [u8], frame_width: usize, x: usize, y: usize, color: [u8; 4]) {
@@ -426,18 +442,20 @@ mod tests {
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,
}],
bars: vec![
OverlayBar {
label: "HEAP",
value: "1024K / 8192K".to_string(),
ratio: 0.125,
warn: false,
},
OverlayBar {
label: "BUDGET",
value: "9.0K/10.0K".to_string(),
ratio: 0.9,
warn: true,
},
],
footer: vec![OverlayMetric {
label: "CRASH",
value: "VM PANIC".to_string(),
@@ -122,7 +122,6 @@ impl HostRunner {
w.request_redraw();
}
}
}
impl ApplicationHandler for HostRunner {
@@ -45,8 +45,10 @@ impl HostStats {
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());
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 {