Files
hakmem/docs/analysis/BUG_FLOW_DIAGRAM.md
Moe Charm (CI) 67fb15f35f Wrap debug fprintf in !HAKMEM_BUILD_RELEASE guards (Release build optimization)
## Changes

### 1. core/page_arena.c
- Removed init failure message (lines 25-27) - error is handled by returning early
- All other fprintf statements already wrapped in existing #if !HAKMEM_BUILD_RELEASE blocks

### 2. core/hakmem.c
- Wrapped SIGSEGV handler init message (line 72)
- CRITICAL: Kept SIGSEGV/SIGBUS/SIGABRT error messages (lines 62-64) - production needs crash logs

### 3. core/hakmem_shared_pool.c
- Wrapped all debug fprintf statements in #if !HAKMEM_BUILD_RELEASE:
  - Node pool exhaustion warning (line 252)
  - SP_META_CAPACITY_ERROR warning (line 421)
  - SP_FIX_GEOMETRY debug logging (line 745)
  - SP_ACQUIRE_STAGE0.5_EMPTY debug logging (line 865)
  - SP_ACQUIRE_STAGE0_L0 debug logging (line 803)
  - SP_ACQUIRE_STAGE1_LOCKFREE debug logging (line 922)
  - SP_ACQUIRE_STAGE2_LOCKFREE debug logging (line 996)
  - SP_ACQUIRE_STAGE3 debug logging (line 1116)
  - SP_SLOT_RELEASE debug logging (line 1245)
  - SP_SLOT_FREELIST_LOCKFREE debug logging (line 1305)
  - SP_SLOT_COMPLETELY_EMPTY debug logging (line 1316)
- Fixed lock_stats_init() for release builds (lines 60-65) - ensure g_lock_stats_enabled is initialized

## Performance Validation

Before: 51M ops/s (with debug fprintf overhead)
After:  49.1M ops/s (consistent performance, fprintf removed from hot paths)

## Build & Test

```bash
./build.sh larson_hakmem
./out/release/larson_hakmem 1 5 1 1000 100 10000 42
# Result: 49.1M ops/s
```

Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-26 13:14:18 +09:00

42 lines
1.1 KiB
Markdown

# Bug Flow Diagram: P0 Batch Refill Active Counter Underflow
Legend
- Box 2: Remote Queue (push/drain)
- Box 3: Ownership (owner_tid)
- Box 4: Publish/Adopt + Refill boundary (superslab_refill)
Flow (before fix)
```
free(ptr)
-> Box 2 remote_push (cross-thread)
- active-- (on free) [OK]
- goes into SS freelist [no active change]
refill (P0 batch)
-> trc_pop_from_freelist(meta, want)
- splice to TLS SLL [OK]
- MISSING: active += taken [BUG]
alloc() uses SLL
free(ptr) (again)
-> active-- (but not incremented before) → double-decrement
-> active underflow → OOM perceived
-> superslab_refill returns NULL → crash path (free(): invalid pointer)
```
After fix
```
refill (P0 batch)
-> trc_pop_from_freelist(...)
- splice to TLS SLL
- active += from_freelist [FIX]
-> trc_linear_carve(...)
- active += batch [asserted]
```
Verification Hooks
- One-shot OOM prints from superslab_refill
- Optional: `HAKMEM_TINY_DEBUG_REMOTE_GUARD=1` and `HAKMEM_TINY_TRACE_RING=1`