use crate::{
constants::WASM_PAGE_SIZE_BYTES,
ledger::{AllocationLedger, AllocationRecord},
};
use serde::Serialize;
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub struct DiagnosticExport {
pub current_generation: u64,
pub records: Vec<DiagnosticRecord>,
}
impl DiagnosticExport {
#[must_use]
#[cfg(any(test, feature = "test-support"))]
pub fn from_ledger(ledger: &AllocationLedger) -> Self {
Self::from_records(ledger.current_generation, ledger.records.iter().cloned())
}
pub(crate) fn from_owned_ledger(ledger: AllocationLedger) -> Self {
Self::from_records(ledger.current_generation, ledger.records.into_iter())
}
fn from_records(
current_generation: u64,
records: impl Iterator<Item = AllocationRecord>,
) -> Self {
Self {
current_generation,
records: records
.map(|allocation| DiagnosticRecord {
allocation,
memory_size: None,
})
.collect(),
}
}
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub struct DiagnosticRecord {
pub allocation: AllocationRecord,
#[serde(skip_serializing_if = "Option::is_none")]
pub memory_size: Option<DiagnosticMemorySize>,
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Serialize)]
pub struct DiagnosticMemorySize {
pub wasm_pages: u64,
}
impl DiagnosticMemorySize {
#[must_use]
pub const fn from_wasm_pages(wasm_pages: u64) -> Self {
Self { wasm_pages }
}
#[must_use]
pub const fn bytes(self) -> u64 {
self.wasm_pages.saturating_mul(WASM_PAGE_SIZE_BYTES)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{AllocationDeclaration, MemoryManagerSlot, ledger::AllocationRecord};
#[test]
fn diagnostic_export_copies_ledger_records() {
let declaration = AllocationDeclaration::new(
"app.users.v1",
MemoryManagerSlot::new(100).expect("usable slot"),
)
.expect("declaration");
let ledger = AllocationLedger {
current_generation: 3,
records: vec![AllocationRecord::from_declaration(&declaration)],
};
let export = DiagnosticExport::from_ledger(&ledger);
assert_eq!(export.current_generation, 3);
assert_eq!(export.records.len(), 1);
assert_eq!(export.records[0].memory_size, None);
assert_eq!(export, DiagnosticExport::from_owned_ledger(ledger));
let wire = serde_json::to_value(&export).expect("diagnostic JSON");
assert_eq!(
wire,
serde_json::json!({
"current_generation": 3,
"records": [{"allocation": {"stable_key": "app.users.v1", "slot": 100}}]
})
);
}
#[test]
fn diagnostic_export_can_include_memory_sizes() {
let declaration = AllocationDeclaration::new(
"app.users.v1",
MemoryManagerSlot::new(100).expect("usable slot"),
)
.expect("declaration");
let ledger = AllocationLedger {
current_generation: 3,
records: vec![AllocationRecord::from_declaration(&declaration)],
};
let mut export = DiagnosticExport::from_ledger(&ledger);
export.records[0].memory_size = Some(DiagnosticMemorySize::from_wasm_pages(2));
assert_eq!(
export.records[0].memory_size,
Some(DiagnosticMemorySize { wasm_pages: 2 })
);
let wire = serde_json::to_value(&export).expect("diagnostic JSON");
assert_eq!(
wire["records"][0]["memory_size"],
serde_json::json!({"wasm_pages": 2})
);
}
}