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§
- Aggregate
Memory Stats - Aggregated memory statistics across all threads
- Memory
Slot - Statistics for a single thread’s memory usage
- Memory
Stats Registry - Global registry for cross-thread memory statistics
- Thread
Memory Stats - 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