Files
hakmem/docs/analysis/DEBUG_LOGGING_POLICY.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

2.7 KiB
Raw Blame History

Debug Logging Policy

統一方針

すべての診断ログは HAKMEM_BUILD_RELEASE フラグで統一的に制御する。

Build Modes

  • Release Build (HAKMEM_BUILD_RELEASE=1): 診断ログ完全無効(性能最優先)

    • -DNDEBUG が定義されると自動的に有効
    • 本番環境・ベンチマーク用
  • Debug Build (HAKMEM_BUILD_RELEASE=0): 診断ログ有効(デバッグ用)

    • デフォルトNDEBUG未定義
    • 環境変数で細かく制御可能

Implementation Pattern

推奨パターンGuard関数

static inline int diagnostic_enabled(void) {
#if HAKMEM_BUILD_RELEASE
    return 0;  // Always disabled in release
#else
    // Check env var in debug builds
    static int enabled = -1;
    if (__builtin_expect(enabled == -1, 0)) {
        const char* env = getenv("HAKMEM_DEBUG_FEATURE");
        enabled = (env && *env != '0') ? 1 : 0;
    }
    return enabled;
#endif
}

// Usage
if (__builtin_expect(diagnostic_enabled(), 0)) {
    fprintf(stderr, "[DEBUG] ...\n");
}

避けるパターン

// 悪い例環境変数を毎回チェックgetenv() は遅い)
const char* env = getenv("HAKMEM_DEBUG");
if (env && *env != '0') {
    fprintf(stderr, "...\n");
}

// 悪い例無条件ログrelease でも出力される)
fprintf(stderr, "[DEBUG] ...\n");

Existing Guard Functions

関数 用途 ファイル
trc_refill_guard_enabled() Refill path 診断 core/tiny_refill_opt.h
g_debug_remote_guard Remote queue 診断 core/superslab/superslab_inline.h
tiny_refill_failfast_level() Fail-fast 検証 core/hakmem_tiny_free.inc

Priority for Conversion

  1. 🔥 Hot Path (最優先): Refill, Alloc, Free の fast path 完了
  2. ⚠️ Medium: Remote drain, Magazine 層
  3. Low: Initialization, Slow path

Status

  • trc_refill_guard_enabled() - Release build で完全無効化
  • 残り 92 箇所 - 必要に応じて対処

Makefile Integration

現状:NDEBUG が定義されていない → HAKMEM_BUILD_RELEASE=0

TODO: Release ビルドターゲットに -DNDEBUG を追加

release: CFLAGS += -DNDEBUG -O3

Environment Variables (Debug Build Only)

  • HAKMEM_TINY_REFILL_FAILFAST: Refill path 検証 (0=off, 1=on, 2=verbose)
  • HAKMEM_TINY_REFILL_OPT_DEBUG: Refill 最適化ログ
  • HAKMEM_DEBUG_REMOTE_GUARD: Remote queue 検証

Performance Impact

状態 Throughput 改善
Before (診断あり) 1,015,347 ops/s -
After (guard追加) 1,046,392 ops/s +3.1%
Target (完全無効化) TBD 推定 +5-10%