Fix include order in hakmem.c - move hak_kpi_util.inc.h before hak_core_init.inc.h

Problem: hak_core_init.inc.h references KPI measurement variables
(g_latency_histogram, g_latency_samples, g_baseline_soft_pf, etc.)
but hakmem.c was including hak_kpi_util.inc.h AFTER hak_core_init.inc.h,
causing undefined reference errors.

Solution: Reorder includes so hak_kpi_util.inc.h (definition) comes
before hak_core_init.inc.h (usage).

Build result:  Success (libhakmem.so 547KB, 0 errors)

Minor changes:
- Added extern __thread declarations for TLS SLL debug variables
- Added signal handler logging for debug_dump_last_push
- Improved hakmem_tiny.c structure for Phase 2 preparation

🤖 Generated with Claude Code + Task Agent

Co-Authored-By: Gemini <gemini@example.com>
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Moe Charm (CI)
2025-12-03 13:28:44 +09:00
parent b5be708b6a
commit 2dc9d5d596
21 changed files with 364 additions and 210 deletions

View File

@ -39,6 +39,26 @@ extern uint64_t g_bytes_allocated; // from hakmem_tiny_superslab.c
// Tiny allocator configuration, debug counters, and return helpers
#include "hakmem_tiny_config_box.inc"
// ============================================================================
// Debug: TLS SLL last push tracking (for core/box/tls_sll_box.h)
// ============================================================================
__thread hak_base_ptr_t s_tls_sll_last_push[TINY_NUM_CLASSES] = {0};
#if !HAKMEM_BUILD_RELEASE
// Helper to dump last push from core/hakmem.c (SEGV handler)
// Must be visible to other TUs (extern in hakmem_tiny.h or similar if needed,
// but SEGV handler is in core/hakmem.c which can dlsym or weak link it)
__attribute__((noinline))
void tiny_debug_dump_last_push(int cls) {
hak_base_ptr_t p = s_tls_sll_last_push[cls];
void* raw = HAK_BASE_TO_RAW(p);
fprintf(stderr, "[DEBUG] s_tls_sll_last_push[%d] = %p\n", cls, raw);
if (raw && (uintptr_t)raw > 4096) {
unsigned long* vals = (unsigned long*)raw;
fprintf(stderr, "[DEBUG] Memory at %p: %016lx %016lx\n", raw, vals[0], vals[1]);
}
}
#endif
// Forward declarations for static helpers used before definition
struct TinySlab; // forward
static void move_to_free_list(int class_idx, struct TinySlab* target_slab);
@ -99,9 +119,9 @@ void* __attribute__((cold, noinline)) hak_tiny_alloc_slow(size_t size, int class
static void* __attribute__((cold, noinline)) hak_tiny_alloc_slow(size_t size, int class_idx);
#endif
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
// Box: adopt_gate_try (implementation moved from header for robust linkage)
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
#include "box/adopt_gate_box.h"
extern SuperSlab* g_super_reg_by_class[TINY_NUM_CLASSES][SUPER_REG_PER_CLASS];
extern int g_super_reg_class_size[TINY_NUM_CLASSES];
@ -161,16 +181,16 @@ SuperSlab* adopt_gate_try(int class_idx, TinyTLSSlab* tls) {
}
// ============================================================================
// ============================================================================
// Global State - EXTRACTED to hakmem_tiny_globals_box.inc
// ============================================================================
// ============================================================================
#include "hakmem_tiny_globals_box.inc"
#include "hakmem_tiny_publish_box.inc"
// ============================================================================
// ============================================================================
// EXTRACTED TO hakmem_tiny_fastcache.inc.h (Phase 2D-1)
// ============================================================================
// ============================================================================
// Functions: tiny_fast_pop(), tiny_fast_push() - 28 lines (lines 377-404)
// Forward declarations for functions defined in hakmem_tiny_fastcache.inc.h
static inline void* tiny_fast_pop(int class_idx);
@ -178,34 +198,34 @@ static inline int tiny_fast_push(int class_idx, void* ptr);
static inline void* fastcache_pop(int class_idx);
static inline int fastcache_push(int class_idx, void* ptr);
// ============================================================================
// ============================================================================
// EXTRACTED TO hakmem_tiny_hot_pop.inc.h (Phase 2D-1)
// ============================================================================
// ============================================================================
// Functions: tiny_hot_pop_class0(), tiny_hot_pop_class1(), tiny_hot_pop_class2(), tiny_hot_pop_class3()
// 88 lines (lines 407-494)
// ============================================================================
// ============================================================================
// Legacy Slow Allocation Path - EXTRACTED to hakmem_tiny_legacy_slow_box.inc
// ============================================================================
// ============================================================================
#include "hakmem_tiny_legacy_slow_box.inc"
// ============================================================================
// ============================================================================
// EXTRACTED TO hakmem_tiny_refill.inc.h (Phase 2D-1)
// ============================================================================
// ============================================================================
// Function: tiny_fast_refill_and_take() - 39 lines (lines 584-622)
// ============================================================================
// ============================================================================
// TLS/Frontend State & Configuration - EXTRACTED to hakmem_tiny_tls_state_box.inc
// ============================================================================
// ============================================================================
#include "hakmem_tiny_tls_state_box.inc"
#include "hakmem_tiny_intel.inc"
// ============================================================================
// ============================================================================
// EXTRACTED TO hakmem_tiny_rss.c (Phase 2B-2)
// ============================================================================
// ============================================================================
// EXTRACTED: static int get_rss_kb_self(void) {
// EXTRACTED: FILE* f = fopen("/proc/self/status", "r");
// EXTRACTED: if (!f) return 0;
@ -294,9 +314,9 @@ enum { HAK_TIER_SLL=1, HAK_TIER_MAG=2, HAK_TIER_SLAB=3, HAK_TIER_SUPER=4, HAK_TI
// Background refill workers and intelligence engine
#include "hakmem_tiny_background.inc"
// ============================================================================
// ============================================================================
// EXTRACTED TO hakmem_tiny_fastcache.inc.h (Phase 2D-1)
// ============================================================================
// ============================================================================
// Functions: fastcache_pop(), fastcache_push(), quick_pop() - 25 lines (lines 873-896)
// Ultra-fast try-only variant: attempt a direct SuperSlab bump/freelist pop
@ -320,46 +340,45 @@ static inline void* hak_tiny_alloc_superslab_try_fast(int class_idx) {
return NULL;
}
// ============================================================================
// ============================================================================
// EXTRACTED TO hakmem_tiny_refill.inc.h (Phase 2D-1)
// ============================================================================
// ============================================================================
// Functions: quick_refill_from_sll(), quick_refill_from_mag() - 31 lines (lines 918-949)
// ============================================================================
// ============================================================================
// EXTRACTED TO hakmem_tiny_refill.inc.h (Phase 2D-1)
// ============================================================================
// ============================================================================
// Function: sll_refill_small_from_ss() - 45 lines (lines 952-996)
// Phase 2C-3: TLS operations module (included after helper function definitions)
#include "hakmem_tiny_tls_ops.h"
// New TLS list refill: owner-only bulk take from TLS-cached SuperSlab slab
// ============================================================================
// ============================================================================
// EXTRACTED TO hakmem_tiny_tls_ops.h (Phase 2C-3)
// ============================================================================
// ============================================================================
// Function: tls_refill_from_tls_slab() - 101 lines
// Hot path refill operation, moved to inline function in header
// ============================================================================
// ============================================================================
// EXTRACTED TO hakmem_tiny_tls_ops.h (Phase 2C-3)
// ============================================================================
// ============================================================================
// Function: tls_list_spill_excess() - 97 lines
// Hot path spill operation, moved to inline function in header
// ============================================================================
// ============================================================================
// EXTRACTED TO hakmem_tiny_refill.inc.h (Phase 2D-1)
// ============================================================================
// ============================================================================
// Function: superslab_tls_bump_fast() - 45 lines (lines 1016-1060)
// ============================================================================
// ============================================================================
// EXTRACTED TO hakmem_tiny_refill.inc.h (Phase 2D-1)
// ============================================================================
// ============================================================================
// Function: frontend_refill_fc() - 44 lines (lines 1063-1106)
// SLL capacity policy: for hot tiny classes (0..3), allow larger SLL up to multiplier * mag_cap
// for >=4 keep current conservative half (to limit footprint).
@ -367,25 +386,25 @@ static inline void* hak_tiny_alloc_superslab_try_fast(int class_idx) {
#include "hakmem_tiny_sll_cap_box.inc"
// ============================================================================
// ============================================================================
// EXTRACTED TO hakmem_tiny_refill.inc.h (Phase 2D-1)
// ============================================================================
// ============================================================================
// Function: bulk_mag_to_sll_if_room() - 22 lines (lines 1133-1154)
// Ultra-Mode Batch Configuration - REMOVED (dead code cleanup 2025-11-27)
#include "hakmem_tiny_remote.inc"
// ============================================================================
// ============================================================================
// Internal Helpers
// ============================================================================
// ============================================================================
// Step 2: Slab Registry Operations
// Hash function for slab_base (64KB aligned)
// ============================================================================
// ============================================================================
// EXTRACTED TO hakmem_tiny_registry.c (Phase 2B-3)
// ============================================================================
// ============================================================================
// EXTRACTED: static inline int registry_hash(uintptr_t slab_base) {
// EXTRACTED: return (slab_base >> 16) & SLAB_REGISTRY_MASK;
// EXTRACTED: }
@ -443,9 +462,9 @@ static inline void* hak_tiny_alloc_superslab_try_fast(int class_idx) {
// Lookup slab by base address (O(1) average)
// ============================================================================
// ============================================================================
// Registry Lookup & Owner Slab Discovery - EXTRACTED to hakmem_tiny_slab_lookup_box.inc
// ============================================================================
// ============================================================================
#include "hakmem_tiny_slab_lookup_box.inc"
@ -455,27 +474,27 @@ static inline void* hak_tiny_alloc_superslab_try_fast(int class_idx) {
// Function: move_to_free_list() - 20 lines (lines 1126-1145)
// Move slab to free list
// ============================================================================
// ============================================================================
// Public API
// ============================================================================
// ============================================================================
// ============================================================================
// ============================================================================
// Phase 2D-2: Initialization function (extracted to hakmem_tiny_init.inc)
// ============================================================================
// ============================================================================
#include "hakmem_tiny_init.inc"
// ============================================================================
// ============================================================================
// 3-Layer Architecture (2025-11-01 Simplification)
// ============================================================================
// ============================================================================
// Layer 1: TLS Bump Allocator (ultra-fast, 2-3 instructions/op)
#include "hakmem_tiny_bump.inc.h"
// Layer 2: TLS Small Magazine (fast, 5-10 instructions/op)
#include "hakmem_tiny_smallmag.inc.h"
// ============================================================================
// ============================================================================
// Phase 6 Fast Path Options (mutually exclusive)
// ============================================================================
// ============================================================================
// Choose ONE of the following Phase 6 optimizations:
//
// Phase 6-1.5: Alignment Guessing (LEGACY - committed 2025-11-02)
@ -492,7 +511,7 @@ static inline void* hak_tiny_alloc_superslab_try_fast(int class_idx) {
// - Method: Store pool_type + size_class in 8-byte header
// - Benefit: Extends to ALL pools (Tiny/Mid/L25/Whale)
// - Eliminates: Registry lookups, mid_lookup, owner checks
// ============================================================================
// ============================================================================
// Forward declarations for Phase 6 alloc/free functions
#ifdef HAKMEM_TINY_PHASE6_ULTRA_SIMPLE
@ -505,9 +524,9 @@ static inline void* hak_tiny_alloc_superslab_try_fast(int class_idx) {
#endif
// ============================================================================
// ============================================================================
// Phase 6 Wrapper Functions - EXTRACTED to hakmem_tiny_phase6_wrappers_box.inc
// ============================================================================
// ============================================================================
#include "hakmem_tiny_phase6_wrappers_box.inc"
@ -538,9 +557,9 @@ __attribute__((weak)) int sll_refill_batch_from_ss(int class_idx, int max_take)
}
#endif
// ============================================================================
// ============================================================================
// EXTRACTED TO hakmem_tiny_lifecycle.inc (Phase 2D-3)
// ============================================================================
// ============================================================================
// Function: hak_tiny_trim() - 116 lines (lines 1164-1279)
// Public trim and cleanup operation for lifecycle management
@ -565,9 +584,9 @@ static inline int ultra_validate_sll_head(int class_idx, void* head) {
extern __thread int g_tls_in_wrapper;
#endif
// ============================================================================
// ============================================================================
// EXTRACTED TO hakmem_tiny_lifecycle.inc (Phase 2D-3)
// ============================================================================
// ============================================================================
// Function: tiny_tls_cache_drain() - 90 lines (lines 1314-1403)
// Static function for draining TLS caches
//
@ -643,7 +662,7 @@ static void tiny_tls_sll_diag_atexit(void) {
}
// ============================================================================
// ============================================================================
// ACE Learning Layer & Tiny Guard - EXTRACTED to hakmem_tiny_ace_guard_box.inc
// ============================================================================
#include "hakmem_tiny_ace_guard_box.inc"
// ============================================================================
#include "hakmem_tiny_ace_guard_box.inc"