menios icon indicating copy to clipboard operation
menios copied to clipboard

SQLite-based Hibernate Metadata Management

Open pbalduino opened this issue 3 months ago • 1 comments

Description

Implement SQLite database integration for storing hibernation metadata, providing a robust and queryable way to manage process information, memory mappings, file handles, and system state during hibernate/resume cycles.

Overview

SQLite provides the perfect solution for hibernate metadata:

  • Reliable persistence across reboots
  • ACID transactions ensure consistency
  • SQL queries for complex state reconstruction
  • Embedded (no external dependencies)
  • Well-tested and battle-hardened

Database Schema

Tables

hibernate_snapshot - Overall snapshot metadata

  • snapshot_id (primary key)
  • timestamp
  • kernel_version
  • total_processes
  • total_memory_kb
  • blob_file_path

hibernate_processes - Per-process information

  • pid
  • parent_pid
  • name
  • state (running, sleeping, etc)
  • registers_offset (in binary blob)
  • memory_start, memory_size
  • cwd_path
  • snapshot_id (foreign key)

hibernate_memory_map - Memory page mappings

  • pid
  • virtual_addr
  • physical_addr (optional)
  • page_size
  • permissions (rwx)
  • flags (dirty, shared, etc)
  • blob_offset (where in binary file)
  • snapshot_id (foreign key)
  • Primary key: (pid, virtual_addr, snapshot_id)

hibernate_files - Open file descriptors

  • pid
  • fd (file descriptor number)
  • path
  • position (file offset)
  • flags (O_RDONLY, O_WRONLY, etc)
  • snapshot_id (foreign key)

hibernate_signals - Signal handlers

  • pid
  • signal_number
  • handler_address
  • flags
  • snapshot_id (foreign key)

Indexes

Create indexes on commonly queried columns:

  • (snapshot_id) on all tables
  • (pid) on process-related tables
  • (pid, virtual_addr) on memory_map

Implementation

Save Phase

  1. Create new snapshot record with metadata
  2. Begin transaction
  3. For each process, insert into hibernate_processes
  4. Insert memory mappings into hibernate_memory_map
  5. Insert open files into hibernate_files
  6. Insert signal handlers into hibernate_signals
  7. Commit transaction

Restore Phase

  1. Open SQLite database from hibernate file
  2. Query latest snapshot_id
  3. Reconstruct process list from hibernate_processes
  4. For each process:
    • Query memory mappings
    • Query open files
    • Query signal handlers
  5. Use metadata to guide memory and state restoration

SQLite Integration

Userspace Library

  • Port SQLite3 to meniOS (or use minimal build)
  • Create hibernate-specific wrapper API
  • Handle file I/O through meniOS VFS

Kernel Integration Options

Option A: Userspace Daemon (Recommended)

  • Hibernate daemon runs in userspace
  • Uses libc + SQLite
  • Coordinates with kernel via syscalls
  • Kernel provides memory snapshot, daemon manages metadata

Option B: Kernel SQLite

  • Embed minimal SQLite in kernel
  • More complex, kernel can't easily use standard SQLite
  • Not recommended unless absolutely necessary

API

High-level API for hibernate metadata operations.

Tasks

  • Port SQLite3 to meniOS userspace
  • Create database schema
  • Implement snapshot creation functions
  • Implement query/restore functions
  • Add transaction management
  • Create helper functions for common queries
  • Add database validation and repair
  • Implement version migration (if schema changes)
  • Add database compression (optional)
  • Create debugging/inspection tools

Acceptance Criteria

  • SQLite library integrated into meniOS
  • Database schema created and documented
  • Can save complete system state to database
  • Can query and restore state from database
  • Transactions ensure consistency
  • Database file validates correctly
  • Performance acceptable (metadata operations under 1 second)
  • Error handling for database corruption
  • Tools for inspecting hibernate database

Priority

High - Required for hibernation metadata

Estimated Effort

1-2 weeks

Notes

  • SQLite is ~150KB library, reasonable for meniOS
  • Use in-memory mode during save, flush to disk at end
  • Consider using WAL mode for better crash resistance
  • This is the perfect use case for SQLite - NOT performance critical

pbalduino avatar Oct 12 '25 04:10 pbalduino