1use crate::{
2 constants::WASM_PAGE_SIZE_BYTES,
3 declaration::AllocationDeclaration,
4 ledger::{AllocationLedger, AllocationRecord, GenerationRecord},
5 physical::CommitStoreDiagnostic,
6 slot::{AllocationSlotDescriptor, MemoryManagerAuthorityRecord, MemoryManagerRangeAuthority},
7};
8use serde::{Deserialize, Serialize};
9use std::collections::BTreeMap;
10
11#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
16#[serde(deny_unknown_fields)]
17pub struct DiagnosticExport {
18 pub current_generation: u64,
20 pub ledger_anchor: AllocationSlotDescriptor,
22 pub records: Vec<DiagnosticRecord>,
24 pub generations: Vec<DiagnosticGeneration>,
26 pub commit_recovery: Option<CommitStoreDiagnostic>,
28}
29
30#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
42#[serde(deny_unknown_fields)]
43pub struct DefaultMemoryManagerDoctorReport {
44 pub bootstrapped: bool,
46 pub ledger_anchor: AllocationSlotDescriptor,
48 pub stable_cell: DiagnosticStableCell,
50 pub commit_recovery: Option<CommitStoreDiagnostic>,
52 pub ledger: Option<DiagnosticExport>,
54 pub registered_declarations: Vec<DiagnosticDeclaration>,
56 pub range_authority: DiagnosticRangeAuthority,
59 pub validation: DiagnosticCheck,
65}
66
67#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
74#[serde(deny_unknown_fields)]
75pub struct DiagnosticDeclaration {
76 pub declaring_crate: String,
78 pub declaration: AllocationDeclaration,
80}
81
82impl DiagnosticDeclaration {
83 #[must_use]
85 pub fn new(declaring_crate: impl Into<String>, declaration: AllocationDeclaration) -> Self {
86 Self {
87 declaring_crate: declaring_crate.into(),
88 declaration,
89 }
90 }
91}
92
93#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
100#[serde(deny_unknown_fields)]
101pub struct DiagnosticRangeAuthority {
102 pub registered_records: Vec<MemoryManagerAuthorityRecord>,
104 pub effective_authority: Option<MemoryManagerRangeAuthority>,
107 #[serde(default, skip_serializing_if = "Option::is_none")]
109 pub error: Option<String>,
110}
111
112impl DiagnosticRangeAuthority {
113 #[must_use]
115 pub const fn new(
116 registered_records: Vec<MemoryManagerAuthorityRecord>,
117 effective_authority: Option<MemoryManagerRangeAuthority>,
118 error: Option<String>,
119 ) -> Self {
120 Self {
121 registered_records,
122 effective_authority,
123 error,
124 }
125 }
126}
127
128#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
135#[serde(deny_unknown_fields)]
136pub struct DiagnosticStableCell {
137 pub status: DiagnosticStableCellStatus,
139 pub memory_size: DiagnosticMemorySize,
141 #[serde(default, skip_serializing_if = "Option::is_none")]
143 pub error: Option<String>,
144}
145
146impl DiagnosticStableCell {
147 #[must_use]
149 pub const fn new(
150 status: DiagnosticStableCellStatus,
151 memory_size: DiagnosticMemorySize,
152 error: Option<String>,
153 ) -> Self {
154 Self {
155 status,
156 memory_size,
157 error,
158 }
159 }
160}
161
162#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
169pub enum DiagnosticStableCellStatus {
170 Empty,
172 Readable,
174 Corrupt,
177}
178
179#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
186#[serde(deny_unknown_fields)]
187pub struct DiagnosticCheck {
188 pub status: DiagnosticCheckStatus,
190 #[serde(default, skip_serializing_if = "Option::is_none")]
192 pub message: Option<String>,
193}
194
195impl DiagnosticCheck {
196 #[must_use]
198 pub const fn passed() -> Self {
199 Self {
200 status: DiagnosticCheckStatus::Passed,
201 message: None,
202 }
203 }
204
205 #[must_use]
207 pub fn failed(message: impl Into<String>) -> Self {
208 Self {
209 status: DiagnosticCheckStatus::Failed,
210 message: Some(message.into()),
211 }
212 }
213
214 #[must_use]
216 pub fn not_run(message: impl Into<String>) -> Self {
217 Self {
218 status: DiagnosticCheckStatus::NotRun,
219 message: Some(message.into()),
220 }
221 }
222}
223
224#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
231pub enum DiagnosticCheckStatus {
232 NotRun,
234 Passed,
236 Failed,
238}
239
240impl DiagnosticExport {
241 #[must_use]
243 pub fn from_ledger(ledger: &AllocationLedger, ledger_anchor: AllocationSlotDescriptor) -> Self {
244 Self::from_ledger_with_commit_recovery(ledger, ledger_anchor, None)
245 }
246
247 #[must_use]
249 pub fn from_ledger_with_commit_recovery(
250 ledger: &AllocationLedger,
251 ledger_anchor: AllocationSlotDescriptor,
252 commit_recovery: Option<CommitStoreDiagnostic>,
253 ) -> Self {
254 Self::from_ledger_with_commit_recovery_and_memory_sizes(
255 ledger,
256 ledger_anchor,
257 commit_recovery,
258 std::iter::empty(),
259 )
260 }
261
262 #[must_use]
264 pub fn from_ledger_with_memory_sizes(
265 ledger: &AllocationLedger,
266 ledger_anchor: AllocationSlotDescriptor,
267 memory_sizes: impl IntoIterator<Item = (AllocationSlotDescriptor, DiagnosticMemorySize)>,
268 ) -> Self {
269 Self::from_ledger_with_commit_recovery_and_memory_sizes(
270 ledger,
271 ledger_anchor,
272 None,
273 memory_sizes,
274 )
275 }
276
277 #[must_use]
279 pub fn from_ledger_with_commit_recovery_and_memory_sizes(
280 ledger: &AllocationLedger,
281 ledger_anchor: AllocationSlotDescriptor,
282 commit_recovery: Option<CommitStoreDiagnostic>,
283 memory_sizes: impl IntoIterator<Item = (AllocationSlotDescriptor, DiagnosticMemorySize)>,
284 ) -> Self {
285 let memory_sizes: BTreeMap<_, _> = memory_sizes.into_iter().collect();
286 Self {
287 current_generation: ledger.current_generation,
288 ledger_anchor,
289 records: ledger
290 .allocation_history()
291 .records()
292 .iter()
293 .cloned()
294 .map(|allocation| {
295 let memory_size = memory_sizes.get(allocation.slot()).copied();
296 DiagnosticRecord {
297 allocation,
298 memory_size,
299 }
300 })
301 .collect(),
302 generations: ledger
303 .allocation_history()
304 .generations()
305 .iter()
306 .cloned()
307 .map(|generation| DiagnosticGeneration { generation })
308 .collect(),
309 commit_recovery,
310 }
311 }
312}
313
314#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
319#[serde(deny_unknown_fields)]
320pub struct DiagnosticRecord {
321 pub allocation: AllocationRecord,
323 #[serde(default, skip_serializing_if = "Option::is_none")]
328 pub memory_size: Option<DiagnosticMemorySize>,
329}
330
331#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
338#[serde(deny_unknown_fields)]
339pub struct DiagnosticMemorySize {
340 pub wasm_pages: u64,
342 pub bytes: u64,
344}
345
346impl DiagnosticMemorySize {
347 #[must_use]
349 pub const fn from_wasm_pages(wasm_pages: u64) -> Self {
350 Self {
351 wasm_pages,
352 bytes: wasm_pages.saturating_mul(WASM_PAGE_SIZE_BYTES),
353 }
354 }
355}
356
357#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
362#[serde(deny_unknown_fields)]
363pub struct DiagnosticGeneration {
364 pub generation: GenerationRecord,
366}
367
368#[cfg(test)]
369mod tests {
370 use super::*;
371 use crate::{
372 declaration::AllocationDeclaration,
373 ledger::{AllocationHistory, AllocationRecord, AllocationState},
374 physical::{CommitRecoveryError, CommitSlotDiagnostic, CommitStoreDiagnostic},
375 schema::SchemaMetadata,
376 };
377
378 #[test]
379 fn diagnostic_export_copies_ledger_records() {
380 let declaration = AllocationDeclaration::new(
381 "app.users.v1",
382 AllocationSlotDescriptor::memory_manager(100).expect("usable slot"),
383 None,
384 SchemaMetadata::default(),
385 )
386 .expect("declaration");
387 let ledger = AllocationLedger {
388 current_generation: 3,
389 allocation_history: AllocationHistory::from_parts(
390 vec![AllocationRecord::from_declaration(
391 3,
392 declaration,
393 AllocationState::Active,
394 )],
395 vec![GenerationRecord {
396 generation: 3,
397 parent_generation: 2,
398 runtime_fingerprint: Some("wasm:abc123".to_string()),
399 declaration_count: 1,
400 committed_at: None,
401 }],
402 ),
403 };
404
405 let export = DiagnosticExport::from_ledger(
406 &ledger,
407 AllocationSlotDescriptor::memory_manager(0).expect("usable slot"),
408 );
409
410 assert_eq!(export.current_generation, 3);
411 assert_eq!(export.records.len(), 1);
412 assert_eq!(export.records[0].memory_size, None);
413 assert_eq!(export.generations.len(), 1);
414 assert_eq!(
415 export.ledger_anchor,
416 AllocationSlotDescriptor::memory_manager(0).expect("usable slot")
417 );
418 assert_eq!(export.commit_recovery, None);
419 }
420
421 #[test]
422 fn diagnostic_export_rejects_unknown_top_level_fields() {
423 use serde_cbor::Value;
424
425 let export = DiagnosticExport {
426 current_generation: 0,
427 ledger_anchor: AllocationSlotDescriptor::memory_manager(0).expect("usable slot"),
428 records: Vec::new(),
429 generations: Vec::new(),
430 commit_recovery: None,
431 };
432 let Value::Map(mut map) = serde_cbor::value::to_value(export).expect("diagnostic value")
433 else {
434 panic!("diagnostic export encodes as a map");
435 };
436 map.insert(Value::Text("future_field".to_string()), Value::Bool(true));
437 let bytes = serde_cbor::to_vec(&Value::Map(map)).expect("diagnostic bytes");
438
439 let err = serde_cbor::from_slice::<DiagnosticExport>(&bytes)
440 .expect_err("unknown diagnostic field must fail closed");
441
442 assert!(err.to_string().contains("future_field"));
443 }
444
445 #[test]
446 fn diagnostic_export_can_include_commit_recovery_state() {
447 let ledger = AllocationLedger {
448 current_generation: 3,
449 allocation_history: AllocationHistory::default(),
450 };
451 let commit_recovery = CommitStoreDiagnostic {
452 slot0: CommitSlotDiagnostic {
453 present: true,
454 generation: Some(3),
455 valid: true,
456 },
457 slot1: CommitSlotDiagnostic {
458 present: false,
459 generation: None,
460 valid: false,
461 },
462 authoritative_generation: Some(3),
463 recovery_error: None,
464 };
465
466 let export = DiagnosticExport::from_ledger_with_commit_recovery(
467 &ledger,
468 AllocationSlotDescriptor::memory_manager(0).expect("usable slot"),
469 Some(commit_recovery),
470 );
471
472 assert_eq!(export.commit_recovery, Some(commit_recovery));
473 }
474
475 #[test]
476 fn diagnostic_export_can_include_memory_sizes() {
477 let declaration = AllocationDeclaration::new(
478 "app.users.v1",
479 AllocationSlotDescriptor::memory_manager(100).expect("usable slot"),
480 None,
481 SchemaMetadata::default(),
482 )
483 .expect("declaration");
484 let ledger = AllocationLedger {
485 current_generation: 3,
486 allocation_history: AllocationHistory::from_parts(
487 vec![AllocationRecord::from_declaration(
488 3,
489 declaration,
490 AllocationState::Active,
491 )],
492 Vec::new(),
493 ),
494 };
495
496 let export = DiagnosticExport::from_ledger_with_memory_sizes(
497 &ledger,
498 AllocationSlotDescriptor::memory_manager(0).expect("usable slot"),
499 [(
500 AllocationSlotDescriptor::memory_manager(100).expect("usable slot"),
501 DiagnosticMemorySize::from_wasm_pages(2),
502 )],
503 );
504
505 assert_eq!(
506 export.records[0].memory_size,
507 Some(DiagnosticMemorySize {
508 wasm_pages: 2,
509 bytes: 131_072,
510 })
511 );
512 }
513
514 #[test]
515 fn diagnostic_export_can_report_recovery_failure() {
516 let ledger = AllocationLedger {
517 current_generation: 0,
518 allocation_history: AllocationHistory::default(),
519 };
520 let commit_recovery = CommitStoreDiagnostic {
521 slot0: CommitSlotDiagnostic {
522 present: false,
523 generation: None,
524 valid: false,
525 },
526 slot1: CommitSlotDiagnostic {
527 present: false,
528 generation: None,
529 valid: false,
530 },
531 authoritative_generation: None,
532 recovery_error: Some(CommitRecoveryError::NoValidGeneration),
533 };
534
535 let export = DiagnosticExport::from_ledger_with_commit_recovery(
536 &ledger,
537 AllocationSlotDescriptor::memory_manager(0).expect("usable slot"),
538 Some(commit_recovery),
539 );
540
541 assert_eq!(
542 export
543 .commit_recovery
544 .expect("commit recovery")
545 .recovery_error,
546 Some(CommitRecoveryError::NoValidGeneration)
547 );
548 }
549}