// 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 #include #include #include #include "hakmem_tiny_superslab.h" // For SuperSlab/TinySlabMeta and hak_now_ns // Runtime-configurable fail-fast level // 0 = disabled, 1 = log only, 2 = log + raise(SIGUSR2), 3 = abort() 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; const char* e = getenv("HAKMEM_TINY_REFILL_FAILFAST"); int parsed = (e && *e) ? atoi(e) : 0; if (parsed < 0) parsed = 0; if (parsed > 3) parsed = 3; 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); } }