Skip to main content

ic_memory/runtime/
diagnostics.rs

1use super::{MemoryRuntime, RuntimeDiagnosticError};
2use crate::{
3    DiagnosticExport, DiagnosticMemorySize, RecoveredLedger, physical::CommitSnapshotDiagnostic,
4};
5use ic_stable_structures::Memory;
6
7impl<M: Memory> MemoryRuntime<M> {
8    /// Export this runtime's recovered ledger and live virtual-memory sizes.
9    pub fn diagnostic_export(&self) -> Result<DiagnosticExport, RuntimeDiagnosticError> {
10        if !self.is_bootstrapped() {
11            return Err(RuntimeDiagnosticError::NotBootstrapped);
12        }
13        let recovered = self.ledger_record_from_memory()?.recover()?;
14        Ok(self.recovered_diagnostic_export(recovered))
15    }
16
17    /// Diagnose protected commit recovery from this runtime's ledger memory.
18    ///
19    /// This operation is available before bootstrap when the stable-cell
20    /// envelope is readable or the ledger memory is empty.
21    pub fn commit_recovery_diagnostic(
22        &self,
23    ) -> Result<CommitSnapshotDiagnostic, RuntimeDiagnosticError> {
24        let record = self.ledger_record_from_memory()?;
25        Ok(record.diagnostic())
26    }
27
28    fn recovered_diagnostic_export(&self, recovered: RecoveredLedger) -> DiagnosticExport {
29        let mut export = DiagnosticExport::from_owned_ledger(recovered.into_ledger());
30        for record in &mut export.records {
31            record.memory_size = Some(DiagnosticMemorySize::from_wasm_pages(
32                self.memory_size_pages(record.allocation.slot().id()),
33            ));
34        }
35        export
36    }
37}