Files
hakmem/core/tiny_failfast.c

73 lines
2.4 KiB
C
Raw Permalink Normal View History

// tiny_failfast.c - Fail-fast debugging utilities (lightweight stubs)
// Purpose: Provide link-time definitions for instrumentation hooks used across
// alloc/free/refill paths. Behavior controlled via env variables.
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <stdatomic.h>
#include "hakmem_tiny_superslab.h" // For SuperSlab/TinySlabMeta and hak_now_ns
Cleanup: Consolidate debug ENV vars to HAKMEM_DEBUG_LEVEL Integrated 4 new debug environment variables added during bug fixes into the existing unified HAKMEM_DEBUG_LEVEL system (expanded to 0-5 levels). Changes: 1. Expanded HAKMEM_DEBUG_LEVEL from 0-3 to 0-5 levels: - 0 = OFF (production) - 1 = ERROR (critical errors) - 2 = WARN (warnings) - 3 = INFO (allocation paths, header validation, stats) - 4 = DEBUG (guard instrumentation, failfast) - 5 = TRACE (verbose tracing) 2. Integrated 4 environment variables: - HAKMEM_ALLOC_PATH_TRACE → HAKMEM_DEBUG_LEVEL >= 3 (INFO) - HAKMEM_TINY_SLL_VALIDATE_HDR → HAKMEM_DEBUG_LEVEL >= 3 (INFO) - HAKMEM_TINY_REFILL_FAILFAST → HAKMEM_DEBUG_LEVEL >= 4 (DEBUG) - HAKMEM_TINY_GUARD → HAKMEM_DEBUG_LEVEL >= 4 (DEBUG) 3. Kept 2 special-purpose variables (fine-grained control): - HAKMEM_TINY_GUARD_CLASS (target class for guard) - HAKMEM_TINY_GUARD_MAX (max guard events) 4. Backward compatibility: - Legacy ENV vars still work via hak_debug_check_level() - New code uses unified system - No behavior changes for existing users Updated files: - core/hakmem_debug_master.h (level 0-5 expansion) - core/hakmem_tiny_superslab_internal.h (alloc path trace) - core/box/tls_sll_box.h (header validation) - core/tiny_failfast.c (failfast level) - core/tiny_refill_opt.h (failfast guard) - core/hakmem_tiny_ace_guard_box.inc (guard enable) - core/hakmem_tiny.c (include hakmem_debug_master.h) Impact: - Simpler debug control: HAKMEM_DEBUG_LEVEL=3 instead of 4 separate ENVs - Easier to discover/use - Consistent debug levels across codebase - Reduces ENV variable proliferation (43+ vars surveyed) Future work: - Consolidate remaining 39+ debug variables (documented in survey) - Gradual migration over 2-3 releases 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-29 06:57:03 +09:00
#include "hakmem_debug_master.h" // For unified debug level control
// Runtime-configurable fail-fast level
// 0 = disabled, 1 = log only, 2 = log + raise(SIGUSR2), 3 = abort()
Cleanup: Consolidate debug ENV vars to HAKMEM_DEBUG_LEVEL Integrated 4 new debug environment variables added during bug fixes into the existing unified HAKMEM_DEBUG_LEVEL system (expanded to 0-5 levels). Changes: 1. Expanded HAKMEM_DEBUG_LEVEL from 0-3 to 0-5 levels: - 0 = OFF (production) - 1 = ERROR (critical errors) - 2 = WARN (warnings) - 3 = INFO (allocation paths, header validation, stats) - 4 = DEBUG (guard instrumentation, failfast) - 5 = TRACE (verbose tracing) 2. Integrated 4 environment variables: - HAKMEM_ALLOC_PATH_TRACE → HAKMEM_DEBUG_LEVEL >= 3 (INFO) - HAKMEM_TINY_SLL_VALIDATE_HDR → HAKMEM_DEBUG_LEVEL >= 3 (INFO) - HAKMEM_TINY_REFILL_FAILFAST → HAKMEM_DEBUG_LEVEL >= 4 (DEBUG) - HAKMEM_TINY_GUARD → HAKMEM_DEBUG_LEVEL >= 4 (DEBUG) 3. Kept 2 special-purpose variables (fine-grained control): - HAKMEM_TINY_GUARD_CLASS (target class for guard) - HAKMEM_TINY_GUARD_MAX (max guard events) 4. Backward compatibility: - Legacy ENV vars still work via hak_debug_check_level() - New code uses unified system - No behavior changes for existing users Updated files: - core/hakmem_debug_master.h (level 0-5 expansion) - core/hakmem_tiny_superslab_internal.h (alloc path trace) - core/box/tls_sll_box.h (header validation) - core/tiny_failfast.c (failfast level) - core/tiny_refill_opt.h (failfast guard) - core/hakmem_tiny_ace_guard_box.inc (guard enable) - core/hakmem_tiny.c (include hakmem_debug_master.h) Impact: - Simpler debug control: HAKMEM_DEBUG_LEVEL=3 instead of 4 separate ENVs - Easier to discover/use - Consistent debug levels across codebase - Reduces ENV variable proliferation (43+ vars surveyed) Future work: - Consolidate remaining 39+ debug variables (documented in survey) - Gradual migration over 2-3 releases 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-29 06:57:03 +09:00
// Enabled via HAKMEM_DEBUG_LEVEL >= 4 (DEBUG level)
// Legacy: HAKMEM_TINY_REFILL_FAILFAST still works for compatibility
int tiny_refill_failfast_level(void)
{
static _Atomic int lvl = -1;
int v = atomic_load_explicit(&lvl, memory_order_relaxed);
if (__builtin_expect(v != -1, 1)) return v;
Cleanup: Consolidate debug ENV vars to HAKMEM_DEBUG_LEVEL Integrated 4 new debug environment variables added during bug fixes into the existing unified HAKMEM_DEBUG_LEVEL system (expanded to 0-5 levels). Changes: 1. Expanded HAKMEM_DEBUG_LEVEL from 0-3 to 0-5 levels: - 0 = OFF (production) - 1 = ERROR (critical errors) - 2 = WARN (warnings) - 3 = INFO (allocation paths, header validation, stats) - 4 = DEBUG (guard instrumentation, failfast) - 5 = TRACE (verbose tracing) 2. Integrated 4 environment variables: - HAKMEM_ALLOC_PATH_TRACE → HAKMEM_DEBUG_LEVEL >= 3 (INFO) - HAKMEM_TINY_SLL_VALIDATE_HDR → HAKMEM_DEBUG_LEVEL >= 3 (INFO) - HAKMEM_TINY_REFILL_FAILFAST → HAKMEM_DEBUG_LEVEL >= 4 (DEBUG) - HAKMEM_TINY_GUARD → HAKMEM_DEBUG_LEVEL >= 4 (DEBUG) 3. Kept 2 special-purpose variables (fine-grained control): - HAKMEM_TINY_GUARD_CLASS (target class for guard) - HAKMEM_TINY_GUARD_MAX (max guard events) 4. Backward compatibility: - Legacy ENV vars still work via hak_debug_check_level() - New code uses unified system - No behavior changes for existing users Updated files: - core/hakmem_debug_master.h (level 0-5 expansion) - core/hakmem_tiny_superslab_internal.h (alloc path trace) - core/box/tls_sll_box.h (header validation) - core/tiny_failfast.c (failfast level) - core/tiny_refill_opt.h (failfast guard) - core/hakmem_tiny_ace_guard_box.inc (guard enable) - core/hakmem_tiny.c (include hakmem_debug_master.h) Impact: - Simpler debug control: HAKMEM_DEBUG_LEVEL=3 instead of 4 separate ENVs - Easier to discover/use - Consistent debug levels across codebase - Reduces ENV variable proliferation (43+ vars surveyed) Future work: - Consolidate remaining 39+ debug variables (documented in survey) - Gradual migration over 2-3 releases 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-29 06:57:03 +09:00
// Use unified debug level (>=4 maps to failfast level 1)
int debug_lvl = hak_debug_check_level("HAKMEM_TINY_REFILL_FAILFAST", 4);
int parsed = debug_lvl ? 1 : 0; // Simple on/off based on debug level
atomic_store_explicit(&lvl, parsed, memory_order_relaxed);
return parsed;
}
void tiny_failfast_log(const char* stage,
int class_idx,
SuperSlab* ss,
TinySlabMeta* meta,
void* ptr,
void* prev)
{
if (tiny_refill_failfast_level() < 1) return;
uint64_t ts = hak_now_ns();
fprintf(stderr,
"[FF][%s] ts=%llu cls=%d ss=%p slab_used=%u cap=%u ptr=%p prev=%p\n",
stage ? stage : "?",
(unsigned long long)ts,
class_idx,
(void*)ss,
meta ? (unsigned)meta->used : 0u,
meta ? (unsigned)meta->capacity : 0u,
ptr,
prev);
}
void tiny_failfast_abort_ptr(const char* stage,
SuperSlab* ss,
int slab_idx,
void* ptr,
const char* reason)
{
int lvl = tiny_refill_failfast_level();
if (lvl <= 0) return;
fprintf(stderr,
"[FF-ABORT][%s] ss=%p slab=%d ptr=%p reason=%s\n",
stage ? stage : "?",
(void*)ss,
slab_idx,
ptr,
reason ? reason : "");
if (lvl >= 3) {
abort();
} else if (lvl >= 2) {
raise(SIGUSR2);
}
}