Module memory_stats

Module memory_stats 

Source
Expand description

Cross-thread memory statistics registry

Provides visibility into arena memory usage across all worker threads. Each thread registers itself and updates its own slot with minimal overhead.

§Design

The challenge: Arena is thread-local, but diagnostics runs on a separate signal handler thread. We solve this with a global registry where each thread has an exclusive slot for its stats.

┌─────────────────────────────────────────────────────────┐
│              MemoryStatsRegistry (global)               │
├─────────────────────────────────────────────────────────┤
│ slots: [MemorySlot; MAX_THREADS]                        │
│                                                         │
│  ┌──────────────────┐  ┌──────────────────┐             │
│  │ Slot 0 (Thread A)│  │ Slot 1 (Thread B)│  ...        │
│  │ thread_id: u64   │  │ thread_id: u64   │             │
│  │ arena_bytes: u64 │  │ arena_bytes: u64 │             │
│  └──────────────────┘  └──────────────────┘             │
└─────────────────────────────────────────────────────────┘

§Performance

  • Registration: One-time CAS per thread (on first arena access)
  • Updates: Single atomic store per operation (~1-2 cycles, no contention)
  • Reads: Only during diagnostics (SIGQUIT), iterates all slots

This maintains the “fast path stays fast” principle.

Structs§

AggregateMemoryStats
Aggregated memory statistics across all threads
MemorySlot
Statistics for a single thread’s memory usage
MemoryStatsRegistry
Global registry for cross-thread memory statistics
ThreadMemoryStats
Memory statistics for a single thread

Functions§

get_or_register_slot
Get or register the current thread’s slot index
memory_registry
Get the global memory stats registry
update_arena_stats
Update arena stats for the current thread