menios
menios copied to clipboard
Memory Snapshot and Restore for Hibernation
Description
Implement efficient memory snapshot and restoration mechanisms to save all process memory pages to disk during hibernation and restore them on resume.
Overview
The memory snapshot system must:
- Iterate through all allocated memory pages across all processes
- Efficiently write pages to hibernate file (sequential I/O)
- Track which pages are dirty and need saving
- Restore pages to correct virtual addresses on resume
- Handle buddy allocator blocks and arena structures
Memory Snapshot Strategy
What to Save
- User process pages (code, data, heap, stack)
- Kernel heap (buddy allocator arenas)
- Page tables (to restore virtual memory layout)
- DMA buffers (if any)
What NOT to Save
- Kernel code (loaded from disk on boot)
- Memory-mapped devices
- Temporary kernel stacks
Optimization Strategies
- Only save dirty pages (read-only pages can be reloaded from disk)
- Compress pages using simple RLE or LZ4
- Write pages sequentially for disk I/O efficiency
- Use bitmap to track which pages are saved
Implementation
Snapshot Process
- Walk buddy allocator arenas to find all allocated blocks
- For each block, determine owning process (PID)
- Check page dirty bit in page table
- Write dirty pages to binary blob
- Record mapping in SQLite: (PID, virtual_addr, blob_offset)
Restore Process
- Read SQLite database to get page mappings
- Allocate buddy blocks for each process
- Read pages from binary blob
- Map pages to correct virtual addresses
- Restore page table entries with correct permissions
Key Functions
Snapshot all memory pages to hibernate file Restore memory from hibernate file Check if page is dirty and needs saving
Integration with Buddy Allocator
The buddy allocator makes this easier:
- Clear ownership (each block knows its arena and order)
- Power-of-2 sizes simplify tracking
- Per-order freelists make iteration straightforward
- Arena structure provides natural boundaries
Buddy-Aware Snapshot
Iterate arenas:
- Walk each arena's used blocks
- For each buddy block, check if allocated
- If allocated, get PID from block header
- Write block contents to hibernate file
- Store: (PID, virtual_addr, order, blob_offset)
SQLite Schema
Store memory page mappings and metadata for quick restore lookups.
Tasks
- Implement memory page walker
- Add dirty page detection
- Create sequential page writer to binary blob
- Implement page compression (optional, nice-to-have)
- Add buddy allocator integration
- Create page restore mechanism
- Implement virtual address mapping restoration
- Add integrity validation (checksums)
- Handle shared pages correctly
- Add progress reporting for large snapshots
Acceptance Criteria
- All allocated memory pages captured
- Pages restored to correct virtual addresses
- Page permissions preserved (rwx)
- Process can access restored memory correctly
- Buddy allocator state consistent after restore
- Performance acceptable (e.g., 4GB RAM in under 30 seconds)
- Error handling for I/O failures
- Validation that memory contents match
Priority
Critical - Core hibernation requirement
Estimated Effort
2-3 weeks
Notes
- Depends on buddy allocator completion for optimal implementation
- Page compression can be added later for space savings
- Consider memory-mapped files (can skip, reload from file on restore)