Skip to content

Implement block cache and buffer management system #63

@pbalduino

Description

@pbalduino

Goal

Build a buffer cache/pager system to smooth repeated reads, coalesce writes, and provide centralized eviction/flush policy.

Context

Block caching sits between the block device driver and filesystem layer, providing performance optimization and write coordination.

Definition of Done

  • Buffer cache: Cache frequently accessed disk blocks in memory
  • Read caching: Eliminate repeated reads of the same blocks
  • Write coalescing: Combine multiple writes to the same block
  • Eviction policy: LRU or similar algorithm for cache management
  • Flush policy: Coordinated writeback with configurable timing
  • Dirty tracking: Track modified blocks that need writeback
  • Synchronization: Thread-safe access to cached blocks

Implementation Details

  • Implement hash table for fast block lookup by device and sector
  • Add LRU chain for cache eviction policy
  • Create dirty block tracking and periodic flush
  • Implement read-ahead for sequential access patterns
  • Add write-behind with configurable delays
  • Support both sync and async I/O modes
  • Integrate with block device layer

Cache Features

  • Read-ahead: Prefetch sequential blocks
  • Write coalescing: Merge adjacent or overlapping writes
  • Sync/async modes: Support both blocking and non-blocking I/O
  • Cache pressure: Handle memory pressure by evicting clean blocks
  • Integrity: Ensure data consistency during crashes

Performance Goals

  • Cache hit rate greater than 90% for typical workloads
  • Minimize memory footprint while maximizing effectiveness
  • Reduce I/O latency through read-ahead
  • Optimize write patterns through coalescing

Files to Create

  • block/buffer_cache.h - Buffer cache interface
  • block/buffer_cache.c - Cache implementation
  • block/writeback.c - Dirty block writeback system
  • block/readahead.c - Sequential read optimization

Testing Strategy

  • Measure cache hit rates with various workloads
  • Test data integrity under concurrent access
  • Verify proper writeback under memory pressure
  • Stress test with large file operations

Dependencies

  • Block device driver implementation
  • Memory management for cache allocation
  • Timer system for periodic flush operations

Related Issues

Part of filesystem support stack - builds on block device driver.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions