2025-11-05 12:31:14 +09:00
|
|
|
#include "tiny_debug_ring.h"
|
Add Box 3 (Pointer Conversion Layer) and fix POOL_TLS_PHASE1 default
## Major Changes
### 1. Box 3: Pointer Conversion Module (NEW)
- File: core/box/ptr_conversion_box.h
- Purpose: Unified BASE ↔ USER pointer conversion (single source of truth)
- API: PTR_BASE_TO_USER(), PTR_USER_TO_BASE()
- Features: Zero-overhead inline, debug mode, NULL-safe, class 7 headerless support
- Design: Header-only, fully modular, no external dependencies
### 2. POOL_TLS_PHASE1 Default OFF (CRITICAL FIX)
- File: build.sh
- Change: POOL_TLS_PHASE1 now defaults to 0 (was hardcoded to 1)
- Impact: Eliminates pthread_mutex overhead on every free() (was causing 3.3x slowdown)
- Usage: Set POOL_TLS_PHASE1=1 env var to enable if needed
### 3. Pointer Conversion Fixes (PARTIAL)
- Files: core/box/front_gate_box.c, core/tiny_alloc_fast.inc.h, etc.
- Status: Partial implementation using Box 3 API
- Note: Work in progress, some conversions still need review
### 4. Performance Investigation Report (NEW)
- File: HOTPATH_PERFORMANCE_INVESTIGATION.md
- Findings:
- Hotpath works (+24% vs baseline) after POOL_TLS fix
- Still 9.2x slower than system malloc due to:
* Heavy initialization (23.85% of cycles)
* Syscall overhead (2,382 syscalls per 100K ops)
* Workload mismatch (C7 1KB is 49.8%, but only C5 256B has hotpath)
* 9.4x more instructions than system malloc
### 5. Known Issues
- SEGV at 20K-30K iterations (pre-existing bug, not related to pointer conversions)
- Root cause: Likely active counter corruption or TLS-SLL chain issues
- Status: Under investigation
## Performance Results (100K iterations, 256B)
- Baseline (Hotpath OFF): 7.22M ops/s
- Hotpath ON: 8.98M ops/s (+24% improvement ✓)
- System malloc: 82.2M ops/s (still 9.2x faster)
## Next Steps
- P0: Fix 20K-30K SEGV bug (GDB investigation needed)
- P1: Lazy initialization (+20-25% expected)
- P1: C7 (1KB) hotpath (+30-40% expected, biggest win)
- P2: Reduce syscalls (+15-20% expected)
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-12 01:01:23 +09:00
|
|
|
#include "hakmem_build_flags.h"
|
2025-11-05 12:31:14 +09:00
|
|
|
#include "hakmem_tiny.h"
|
|
|
|
|
#include <signal.h>
|
|
|
|
|
#include <stdatomic.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <ucontext.h>
|
|
|
|
|
|
Add Box 3 (Pointer Conversion Layer) and fix POOL_TLS_PHASE1 default
## Major Changes
### 1. Box 3: Pointer Conversion Module (NEW)
- File: core/box/ptr_conversion_box.h
- Purpose: Unified BASE ↔ USER pointer conversion (single source of truth)
- API: PTR_BASE_TO_USER(), PTR_USER_TO_BASE()
- Features: Zero-overhead inline, debug mode, NULL-safe, class 7 headerless support
- Design: Header-only, fully modular, no external dependencies
### 2. POOL_TLS_PHASE1 Default OFF (CRITICAL FIX)
- File: build.sh
- Change: POOL_TLS_PHASE1 now defaults to 0 (was hardcoded to 1)
- Impact: Eliminates pthread_mutex overhead on every free() (was causing 3.3x slowdown)
- Usage: Set POOL_TLS_PHASE1=1 env var to enable if needed
### 3. Pointer Conversion Fixes (PARTIAL)
- Files: core/box/front_gate_box.c, core/tiny_alloc_fast.inc.h, etc.
- Status: Partial implementation using Box 3 API
- Note: Work in progress, some conversions still need review
### 4. Performance Investigation Report (NEW)
- File: HOTPATH_PERFORMANCE_INVESTIGATION.md
- Findings:
- Hotpath works (+24% vs baseline) after POOL_TLS fix
- Still 9.2x slower than system malloc due to:
* Heavy initialization (23.85% of cycles)
* Syscall overhead (2,382 syscalls per 100K ops)
* Workload mismatch (C7 1KB is 49.8%, but only C5 256B has hotpath)
* 9.4x more instructions than system malloc
### 5. Known Issues
- SEGV at 20K-30K iterations (pre-existing bug, not related to pointer conversions)
- Root cause: Likely active counter corruption or TLS-SLL chain issues
- Status: Under investigation
## Performance Results (100K iterations, 256B)
- Baseline (Hotpath OFF): 7.22M ops/s
- Hotpath ON: 8.98M ops/s (+24% improvement ✓)
- System malloc: 82.2M ops/s (still 9.2x faster)
## Next Steps
- P0: Fix 20K-30K SEGV bug (GDB investigation needed)
- P1: Lazy initialization (+20-25% expected)
- P1: C7 (1KB) hotpath (+30-40% expected, biggest win)
- P2: Reduce syscalls (+15-20% expected)
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-12 01:01:23 +09:00
|
|
|
#if HAKMEM_BUILD_RELEASE && !HAKMEM_DEBUG_VERBOSE
|
2025-11-13 03:53:01 +09:00
|
|
|
// In release builds without verbose debug, provide no-op stubs.
|
|
|
|
|
// These are needed for LTO builds where inline stubs may not be visible.
|
|
|
|
|
void tiny_debug_ring_init(void) {
|
|
|
|
|
// No-op in release builds
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void tiny_debug_ring_record(uint16_t event, uint16_t class_idx, void* ptr, uintptr_t aux) {
|
|
|
|
|
(void)event;
|
|
|
|
|
(void)class_idx;
|
|
|
|
|
(void)ptr;
|
|
|
|
|
(void)aux;
|
|
|
|
|
// No-op in release builds
|
|
|
|
|
}
|
Add Box 3 (Pointer Conversion Layer) and fix POOL_TLS_PHASE1 default
## Major Changes
### 1. Box 3: Pointer Conversion Module (NEW)
- File: core/box/ptr_conversion_box.h
- Purpose: Unified BASE ↔ USER pointer conversion (single source of truth)
- API: PTR_BASE_TO_USER(), PTR_USER_TO_BASE()
- Features: Zero-overhead inline, debug mode, NULL-safe, class 7 headerless support
- Design: Header-only, fully modular, no external dependencies
### 2. POOL_TLS_PHASE1 Default OFF (CRITICAL FIX)
- File: build.sh
- Change: POOL_TLS_PHASE1 now defaults to 0 (was hardcoded to 1)
- Impact: Eliminates pthread_mutex overhead on every free() (was causing 3.3x slowdown)
- Usage: Set POOL_TLS_PHASE1=1 env var to enable if needed
### 3. Pointer Conversion Fixes (PARTIAL)
- Files: core/box/front_gate_box.c, core/tiny_alloc_fast.inc.h, etc.
- Status: Partial implementation using Box 3 API
- Note: Work in progress, some conversions still need review
### 4. Performance Investigation Report (NEW)
- File: HOTPATH_PERFORMANCE_INVESTIGATION.md
- Findings:
- Hotpath works (+24% vs baseline) after POOL_TLS fix
- Still 9.2x slower than system malloc due to:
* Heavy initialization (23.85% of cycles)
* Syscall overhead (2,382 syscalls per 100K ops)
* Workload mismatch (C7 1KB is 49.8%, but only C5 256B has hotpath)
* 9.4x more instructions than system malloc
### 5. Known Issues
- SEGV at 20K-30K iterations (pre-existing bug, not related to pointer conversions)
- Root cause: Likely active counter corruption or TLS-SLL chain issues
- Status: Under investigation
## Performance Results (100K iterations, 256B)
- Baseline (Hotpath OFF): 7.22M ops/s
- Hotpath ON: 8.98M ops/s (+24% improvement ✓)
- System malloc: 82.2M ops/s (still 9.2x faster)
## Next Steps
- P0: Fix 20K-30K SEGV bug (GDB investigation needed)
- P1: Lazy initialization (+20-25% expected)
- P1: C7 (1KB) hotpath (+30-40% expected, biggest win)
- P2: Reduce syscalls (+15-20% expected)
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-12 01:01:23 +09:00
|
|
|
#else
|
|
|
|
|
|
2025-11-05 12:31:14 +09:00
|
|
|
#define TINY_RING_IGNORE(expr) do { ssize_t _tw_ret = (expr); (void)_tw_ret; } while(0)
|
|
|
|
|
|
|
|
|
|
#define TINY_RING_CAP 4096u
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
uintptr_t ptr;
|
|
|
|
|
uintptr_t aux;
|
|
|
|
|
uint16_t event;
|
|
|
|
|
uint16_t class_idx;
|
|
|
|
|
} TinyRingEntry;
|
|
|
|
|
|
|
|
|
|
static TinyRingEntry g_tiny_ring[TINY_RING_CAP];
|
|
|
|
|
static _Atomic uint32_t g_tiny_ring_head = 0;
|
|
|
|
|
static int g_tiny_ring_enabled = 0;
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
const char* name;
|
|
|
|
|
size_t len;
|
|
|
|
|
} TinyRingName;
|
|
|
|
|
|
|
|
|
|
static TinyRingName tiny_ring_event_name(uint16_t event) {
|
|
|
|
|
switch (event) {
|
|
|
|
|
case TINY_RING_EVENT_ALLOC_ENTER: return (TinyRingName){"alloc_enter", 11};
|
|
|
|
|
case TINY_RING_EVENT_ALLOC_SUCCESS: return (TinyRingName){"alloc_ok", 8};
|
|
|
|
|
case TINY_RING_EVENT_ALLOC_NULL: return (TinyRingName){"alloc_null", 10};
|
|
|
|
|
case TINY_RING_EVENT_ALLOC_REFILL_START: return (TinyRingName){"refill_start", 12};
|
|
|
|
|
case TINY_RING_EVENT_ALLOC_REFILL_NULL: return (TinyRingName){"refill_null", 11};
|
|
|
|
|
case TINY_RING_EVENT_ALLOC_BIND: return (TinyRingName){"bind", 4};
|
|
|
|
|
case TINY_RING_EVENT_FREE_ENTER: return (TinyRingName){"free_enter", 10};
|
|
|
|
|
case TINY_RING_EVENT_FREE_FAST: return (TinyRingName){"free_fast", 8};
|
|
|
|
|
case TINY_RING_EVENT_FREE_REMOTE: return (TinyRingName){"free_remote", 11};
|
|
|
|
|
case TINY_RING_EVENT_FREE_LOCAL: return (TinyRingName){"free_local", 10};
|
|
|
|
|
case TINY_RING_EVENT_FREE_RETURN_MAG: return (TinyRingName){"free_mag", 7};
|
|
|
|
|
case TINY_RING_EVENT_SUPERSLAB_ADOPT: return (TinyRingName){"ss_adopt", 8};
|
|
|
|
|
case TINY_RING_EVENT_SUPERSLAB_ALLOC: return (TinyRingName){"ss_alloc", 8};
|
|
|
|
|
case TINY_RING_EVENT_SUPERSLAB_PUBLISH: return (TinyRingName){"ss_publish", 10};
|
|
|
|
|
case TINY_RING_EVENT_SUPERSLAB_ADOPT_FAIL: return (TinyRingName){"ss_adopt_fail", 13};
|
|
|
|
|
case TINY_RING_EVENT_REMOTE_PUSH: return (TinyRingName){"remote_push", 11};
|
|
|
|
|
case TINY_RING_EVENT_REMOTE_INVALID: return (TinyRingName){"remote_invalid", 14};
|
|
|
|
|
case TINY_RING_EVENT_REMOTE_DRAIN: return (TinyRingName){"remote_drain", 12};
|
|
|
|
|
case TINY_RING_EVENT_OWNER_ACQUIRE: return (TinyRingName){"owner_acq", 9};
|
|
|
|
|
case TINY_RING_EVENT_OWNER_RELEASE: return (TinyRingName){"owner_rel", 9};
|
|
|
|
|
case TINY_RING_EVENT_FRONT_BYPASS: return (TinyRingName){"front_bypass", 12};
|
|
|
|
|
case TINY_RING_EVENT_MAILBOX_PUBLISH: return (TinyRingName){"mailbox_publish", 15};
|
|
|
|
|
case TINY_RING_EVENT_MAILBOX_FETCH: return (TinyRingName){"mailbox_fetch", 13};
|
|
|
|
|
case TINY_RING_EVENT_MAILBOX_FETCH_NULL: return (TinyRingName){"mailbox_fetch_null", 18};
|
2025-11-07 01:27:04 +09:00
|
|
|
case TINY_RING_EVENT_ROUTE: return (TinyRingName){"route", 5};
|
Front-Direct implementation: SS→FC direct refill + SLL complete bypass
## Summary
Implemented Front-Direct architecture with complete SLL bypass:
- Direct SuperSlab → FastCache refill (1-hop, bypasses SLL)
- SLL-free allocation/free paths when Front-Direct enabled
- Legacy path sealing (SLL inline opt-in, SFC cascade ENV-only)
## New Modules
- core/refill/ss_refill_fc.h (236 lines): Standard SS→FC refill entry point
- Remote drain → Freelist → Carve priority
- Header restoration for C1-C6 (NOT C0/C7)
- ENV: HAKMEM_TINY_P0_DRAIN_THRESH, HAKMEM_TINY_P0_NO_DRAIN
- core/front/fast_cache.h: FastCache (L1) type definition
- core/front/quick_slot.h: QuickSlot (L0) type definition
## Allocation Path (core/tiny_alloc_fast.inc.h)
- Added s_front_direct_alloc TLS flag (lazy ENV check)
- SLL pop guarded by: g_tls_sll_enable && !s_front_direct_alloc
- Refill dispatch:
- Front-Direct: ss_refill_fc_fill() → fastcache_pop() (1-hop)
- Legacy: sll_refill_batch_from_ss() → SLL → FC (2-hop, A/B only)
- SLL inline pop sealed (requires HAKMEM_TINY_INLINE_SLL=1 opt-in)
## Free Path (core/hakmem_tiny_free.inc, core/hakmem_tiny_fastcache.inc.h)
- FC priority: Try fastcache_push() first (same-thread free)
- tiny_fast_push() bypass: Returns 0 when s_front_direct_free || !g_tls_sll_enable
- Fallback: Magazine/slow path (safe, bypasses SLL)
## Legacy Sealing
- SFC cascade: Default OFF (ENV-only via HAKMEM_TINY_SFC_CASCADE=1)
- Deleted: core/hakmem_tiny_free.inc.bak, core/pool_refill_legacy.c.bak
- Documentation: ss_refill_fc_fill() promoted as CANONICAL refill entry
## ENV Controls
- HAKMEM_TINY_FRONT_DIRECT=1: Enable Front-Direct (SS→FC direct)
- HAKMEM_TINY_P0_DIRECT_FC_ALL=1: Same as above (alt name)
- HAKMEM_TINY_REFILL_BATCH=1: Enable batch refill (also enables Front-Direct)
- HAKMEM_TINY_SFC_CASCADE=1: Enable SFC cascade (default OFF)
- HAKMEM_TINY_INLINE_SLL=1: Enable inline SLL pop (default OFF, requires AGGRESSIVE_INLINE)
## Benchmarks (Front-Direct Enabled)
```bash
ENV: HAKMEM_BENCH_FAST_FRONT=1 HAKMEM_TINY_FRONT_DIRECT=1
HAKMEM_TINY_REFILL_BATCH=1 HAKMEM_TINY_P0_DIRECT_FC_ALL=1
HAKMEM_TINY_REFILL_COUNT_HOT=256 HAKMEM_TINY_REFILL_COUNT_MID=96
HAKMEM_TINY_BUMP_CHUNK=256
bench_random_mixed (16-1040B random, 200K iter):
256 slots: 1.44M ops/s (STABLE, 0 SEGV)
128 slots: 1.44M ops/s (STABLE, 0 SEGV)
bench_fixed_size (fixed size, 200K iter):
256B: 4.06M ops/s (has debug logs, expected >10M without logs)
128B: Similar (debug logs affect)
```
## Verification
- TRACE_RING test (10K iter): **0 SLL events** detected ✅
- Complete SLL bypass confirmed when Front-Direct=1
- Stable execution: 200K iterations × multiple sizes, 0 SEGV
## Next Steps
- Disable debug logs in hak_alloc_api.inc.h (call_num 14250-14280 range)
- Re-benchmark with clean Release build (target: 10-15M ops/s)
- 128/256B shortcut path optimization (FC hit rate improvement)
Co-Authored-By: ChatGPT <chatgpt@openai.com>
Suggested-By: ultrathink
2025-11-14 05:41:49 +09:00
|
|
|
case TINY_RING_EVENT_TLS_SLL_REJECT: return (TinyRingName){"tls_sll_reject", 14};
|
|
|
|
|
case TINY_RING_EVENT_TLS_SLL_SENTINEL: return (TinyRingName){"tls_sll_sentinel", 16};
|
|
|
|
|
case TINY_RING_EVENT_TLS_SLL_HDR_CORRUPT: return (TinyRingName){"tls_sll_hdr_corrupt", 20};
|
2025-11-05 12:31:14 +09:00
|
|
|
default: return (TinyRingName){"unknown", 7};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void tiny_ring_write_dec(int fd, uint64_t value) {
|
|
|
|
|
char buf[32];
|
|
|
|
|
int pos = 31;
|
|
|
|
|
if (value == 0) {
|
|
|
|
|
buf[pos--] = '0';
|
|
|
|
|
} else {
|
|
|
|
|
while (value > 0 && pos >= 0) {
|
|
|
|
|
buf[pos--] = (char)('0' + (value % 10));
|
|
|
|
|
value /= 10;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
int len = 31 - pos;
|
|
|
|
|
TINY_RING_IGNORE(write(fd, buf + pos + 1, len));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void tiny_ring_write_hex(int fd, uintptr_t value) {
|
|
|
|
|
static const char* hex = "0123456789abcdef";
|
|
|
|
|
char buf[2 + sizeof(uintptr_t) * 2 + 1];
|
|
|
|
|
buf[0] = '0';
|
|
|
|
|
buf[1] = 'x';
|
|
|
|
|
for (int i = (int)(sizeof(uintptr_t) * 2) - 1; i >= 0; --i) {
|
|
|
|
|
buf[2 + i] = hex[value & 0xFu];
|
|
|
|
|
value >>= 4;
|
|
|
|
|
}
|
|
|
|
|
buf[2 + sizeof(uintptr_t) * 2] = '\0';
|
|
|
|
|
TINY_RING_IGNORE(write(fd, buf, 2 + sizeof(uintptr_t) * 2));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void tiny_debug_ring_dump(int fd, uintptr_t fault_addr) {
|
|
|
|
|
const char hdr[] = "\n[Tiny Debug Ring Dump]\n";
|
|
|
|
|
TINY_RING_IGNORE(write(fd, hdr, sizeof(hdr) - 1));
|
|
|
|
|
const char addr_prefix[] = "fault_addr=";
|
|
|
|
|
TINY_RING_IGNORE(write(fd, addr_prefix, sizeof(addr_prefix) - 1));
|
|
|
|
|
tiny_ring_write_hex(fd, fault_addr);
|
|
|
|
|
TINY_RING_IGNORE(write(fd, "\n", 1));
|
|
|
|
|
|
|
|
|
|
uint32_t head = atomic_load_explicit(&g_tiny_ring_head, memory_order_relaxed);
|
|
|
|
|
uint32_t count = head < TINY_RING_CAP ? head : TINY_RING_CAP;
|
|
|
|
|
for (uint32_t i = 0; i < count; i++) {
|
|
|
|
|
uint32_t idx = (head - count + i) & (TINY_RING_CAP - 1u);
|
|
|
|
|
TinyRingEntry ent = g_tiny_ring[idx];
|
|
|
|
|
TINY_RING_IGNORE(write(fd, "[", 1));
|
|
|
|
|
tiny_ring_write_dec(fd, idx);
|
|
|
|
|
const char mid[] = "] event=";
|
|
|
|
|
TINY_RING_IGNORE(write(fd, mid, sizeof(mid) - 1));
|
|
|
|
|
TinyRingName name = tiny_ring_event_name(ent.event);
|
|
|
|
|
TINY_RING_IGNORE(write(fd, name.name, name.len));
|
|
|
|
|
const char cls[] = " class=";
|
|
|
|
|
TINY_RING_IGNORE(write(fd, cls, sizeof(cls) - 1));
|
|
|
|
|
tiny_ring_write_dec(fd, ent.class_idx);
|
|
|
|
|
const char ptr_prefix[] = " ptr=";
|
|
|
|
|
TINY_RING_IGNORE(write(fd, ptr_prefix, sizeof(ptr_prefix) - 1));
|
|
|
|
|
tiny_ring_write_hex(fd, ent.ptr);
|
|
|
|
|
const char aux_prefix[] = " aux=";
|
|
|
|
|
TINY_RING_IGNORE(write(fd, aux_prefix, sizeof(aux_prefix) - 1));
|
|
|
|
|
tiny_ring_write_hex(fd, ent.aux);
|
|
|
|
|
TINY_RING_IGNORE(write(fd, "\n", 1));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void tiny_debug_ring_sigsegv(int signo, siginfo_t* info, void* uctx) {
|
|
|
|
|
uintptr_t ip = 0;
|
|
|
|
|
#if defined(__x86_64__)
|
|
|
|
|
if (uctx) {
|
|
|
|
|
ucontext_t* uc = (ucontext_t*)uctx;
|
|
|
|
|
#ifdef REG_RIP
|
|
|
|
|
ip = (uintptr_t)uc->uc_mcontext.gregs[REG_RIP];
|
|
|
|
|
#else
|
|
|
|
|
(void)uc; // REG_RIP not available on this platform
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
if (g_tiny_ring_enabled) {
|
|
|
|
|
uintptr_t fault = info ? (uintptr_t)info->si_addr : 0;
|
|
|
|
|
#if defined(__x86_64__)
|
|
|
|
|
#ifdef REG_RIP
|
|
|
|
|
if (ip != 0) {
|
|
|
|
|
const char rip_prefix[] = "rip=";
|
|
|
|
|
TINY_RING_IGNORE(write(STDERR_FILENO, rip_prefix, sizeof(rip_prefix) - 1));
|
|
|
|
|
tiny_ring_write_hex(STDERR_FILENO, ip);
|
|
|
|
|
TINY_RING_IGNORE(write(STDERR_FILENO, "\n", 1));
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
#endif
|
|
|
|
|
tiny_debug_ring_dump(STDERR_FILENO, fault);
|
|
|
|
|
}
|
|
|
|
|
const char msg[] = "[Tiny Debug Ring] captured SIGSEGV\n";
|
|
|
|
|
TINY_RING_IGNORE(write(STDERR_FILENO, msg, sizeof(msg) - 1));
|
|
|
|
|
#if defined(__x86_64__)
|
|
|
|
|
#ifdef REG_RIP
|
|
|
|
|
if (ip != 0) {
|
|
|
|
|
const char rip_prefix[] = "rip=";
|
|
|
|
|
TINY_RING_IGNORE(write(STDERR_FILENO, rip_prefix, sizeof(rip_prefix) - 1));
|
|
|
|
|
tiny_ring_write_hex(STDERR_FILENO, ip);
|
|
|
|
|
TINY_RING_IGNORE(write(STDERR_FILENO, "\n", 1));
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
#endif
|
|
|
|
|
_exit(128 + signo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void tiny_debug_ring_sigusr(int signo, siginfo_t* info, void* uctx) {
|
|
|
|
|
(void)signo;
|
|
|
|
|
(void)info;
|
|
|
|
|
(void)uctx;
|
|
|
|
|
if (g_tiny_ring_enabled) {
|
|
|
|
|
tiny_debug_ring_dump(STDERR_FILENO, 0);
|
|
|
|
|
const char msg[] = "[Tiny Debug Ring] SIGUSR2 dump\n";
|
|
|
|
|
TINY_RING_IGNORE(write(STDERR_FILENO, msg, sizeof(msg) - 1));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void tiny_debug_ring_init(void) {
|
ENV cleanup: Add RELEASE guards to DEBUG ENV variables (14 vars)
Added compile-time guards (#if HAKMEM_BUILD_RELEASE) to eliminate
DEBUG ENV variable overhead in RELEASE builds.
Variables guarded (14 total):
- HAKMEM_TINY_TRACE_RING, HAKMEM_TINY_DUMP_RING_ATEXIT
- HAKMEM_TINY_RF_TRACE, HAKMEM_TINY_MAILBOX_TRACE
- HAKMEM_TINY_MAILBOX_TRACE_LIMIT, HAKMEM_TINY_MAILBOX_SLOWDISC
- HAKMEM_TINY_MAILBOX_SLOWDISC_PERIOD
- HAKMEM_SS_PREWARM_DEBUG, HAKMEM_SS_FREE_DEBUG
- HAKMEM_TINY_FRONT_METRICS, HAKMEM_TINY_FRONT_DUMP
- HAKMEM_TINY_COUNTERS_DUMP, HAKMEM_TINY_REFILL_DUMP
- HAKMEM_PTR_TRACE_DUMP, HAKMEM_PTR_TRACE_VERBOSE
Files modified (9 core files):
- core/tiny_debug_ring.c (ring trace/dump)
- core/box/mailbox_box.c (mailbox trace + slowdisc)
- core/tiny_refill.h (refill trace)
- core/hakmem_tiny_superslab.c (superslab debug)
- core/box/ss_allocation_box.c (allocation debug)
- core/tiny_superslab_free.inc.h (free debug)
- core/box/front_metrics_box.c (frontend metrics)
- core/hakmem_tiny_stats.c (stats dump)
- core/ptr_trace.h (pointer trace)
Bug fixes during implementation:
1. mailbox_box.c - Fixed variable scope (moved 'used' outside guard)
2. hakmem_tiny_stats.c - Fixed incomplete declarations (on1, on2)
Impact:
- Binary size: -85KB total
- bench_random_mixed_hakmem: 319K → 305K (-14K, -4.4%)
- larson_hakmem: 380K → 309K (-71K, -18.7%)
- Performance: No regression (16.9-17.9M ops/s maintained)
- Functional: All tests pass (Random Mixed + Larson)
- Behavior: DEBUG ENV vars correctly ignored in RELEASE builds
Testing:
- Build: Clean compilation (warnings only, pre-existing)
- 100K Random Mixed: 16.9-17.9M ops/s (PASS)
- 10K Larson: 25.9M ops/s (PASS)
- DEBUG ENV verification: Correctly ignored (PASS)
Result: 14 DEBUG ENV variables now have zero overhead in RELEASE builds.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-27 03:41:07 +09:00
|
|
|
#if HAKMEM_BUILD_RELEASE
|
|
|
|
|
return; // No env reads or hooks in release builds
|
|
|
|
|
#endif
|
2025-11-05 12:31:14 +09:00
|
|
|
if (g_tiny_ring_enabled) return;
|
|
|
|
|
const char* env = getenv("HAKMEM_TINY_TRACE_RING");
|
|
|
|
|
if (!(env && *env && env[0] != '0')) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
g_tiny_ring_enabled = 1;
|
|
|
|
|
struct sigaction sa;
|
|
|
|
|
sigemptyset(&sa.sa_mask);
|
|
|
|
|
sa.sa_flags = SA_SIGINFO | SA_RESETHAND;
|
|
|
|
|
sa.sa_sigaction = tiny_debug_ring_sigsegv;
|
|
|
|
|
sigaction(SIGSEGV, &sa, NULL);
|
|
|
|
|
|
|
|
|
|
struct sigaction su;
|
|
|
|
|
sigemptyset(&su.sa_mask);
|
|
|
|
|
su.sa_flags = SA_SIGINFO | SA_RESTART;
|
|
|
|
|
su.sa_sigaction = tiny_debug_ring_sigusr;
|
|
|
|
|
sigaction(SIGUSR2, &su, NULL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void tiny_debug_ring_record(uint16_t event, uint16_t class_idx, void* ptr, uintptr_t aux) {
|
|
|
|
|
if (!g_tiny_ring_enabled) return;
|
|
|
|
|
uint32_t idx = atomic_fetch_add_explicit(&g_tiny_ring_head, 1u, memory_order_relaxed);
|
|
|
|
|
TinyRingEntry entry;
|
|
|
|
|
entry.ptr = (uintptr_t)ptr;
|
|
|
|
|
entry.aux = aux;
|
|
|
|
|
entry.event = event;
|
|
|
|
|
entry.class_idx = class_idx;
|
|
|
|
|
g_tiny_ring[idx & (TINY_RING_CAP - 1u)] = entry;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
__attribute__((constructor))
|
|
|
|
|
static void tiny_debug_ring_ctor(void) {
|
|
|
|
|
tiny_debug_ring_init();
|
|
|
|
|
}
|
2025-11-07 01:27:04 +09:00
|
|
|
|
|
|
|
|
__attribute__((destructor))
|
|
|
|
|
static void tiny_debug_ring_dtor(void) {
|
ENV cleanup: Add RELEASE guards to DEBUG ENV variables (14 vars)
Added compile-time guards (#if HAKMEM_BUILD_RELEASE) to eliminate
DEBUG ENV variable overhead in RELEASE builds.
Variables guarded (14 total):
- HAKMEM_TINY_TRACE_RING, HAKMEM_TINY_DUMP_RING_ATEXIT
- HAKMEM_TINY_RF_TRACE, HAKMEM_TINY_MAILBOX_TRACE
- HAKMEM_TINY_MAILBOX_TRACE_LIMIT, HAKMEM_TINY_MAILBOX_SLOWDISC
- HAKMEM_TINY_MAILBOX_SLOWDISC_PERIOD
- HAKMEM_SS_PREWARM_DEBUG, HAKMEM_SS_FREE_DEBUG
- HAKMEM_TINY_FRONT_METRICS, HAKMEM_TINY_FRONT_DUMP
- HAKMEM_TINY_COUNTERS_DUMP, HAKMEM_TINY_REFILL_DUMP
- HAKMEM_PTR_TRACE_DUMP, HAKMEM_PTR_TRACE_VERBOSE
Files modified (9 core files):
- core/tiny_debug_ring.c (ring trace/dump)
- core/box/mailbox_box.c (mailbox trace + slowdisc)
- core/tiny_refill.h (refill trace)
- core/hakmem_tiny_superslab.c (superslab debug)
- core/box/ss_allocation_box.c (allocation debug)
- core/tiny_superslab_free.inc.h (free debug)
- core/box/front_metrics_box.c (frontend metrics)
- core/hakmem_tiny_stats.c (stats dump)
- core/ptr_trace.h (pointer trace)
Bug fixes during implementation:
1. mailbox_box.c - Fixed variable scope (moved 'used' outside guard)
2. hakmem_tiny_stats.c - Fixed incomplete declarations (on1, on2)
Impact:
- Binary size: -85KB total
- bench_random_mixed_hakmem: 319K → 305K (-14K, -4.4%)
- larson_hakmem: 380K → 309K (-71K, -18.7%)
- Performance: No regression (16.9-17.9M ops/s maintained)
- Functional: All tests pass (Random Mixed + Larson)
- Behavior: DEBUG ENV vars correctly ignored in RELEASE builds
Testing:
- Build: Clean compilation (warnings only, pre-existing)
- 100K Random Mixed: 16.9-17.9M ops/s (PASS)
- 10K Larson: 25.9M ops/s (PASS)
- DEBUG ENV verification: Correctly ignored (PASS)
Result: 14 DEBUG ENV variables now have zero overhead in RELEASE builds.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-27 03:41:07 +09:00
|
|
|
#if HAKMEM_BUILD_RELEASE
|
|
|
|
|
return; // Skip env access in release builds
|
|
|
|
|
#endif
|
2025-11-07 01:27:04 +09:00
|
|
|
const char* e = getenv("HAKMEM_TINY_DUMP_RING_ATEXIT");
|
|
|
|
|
if (e && *e && e[0] != '0') {
|
|
|
|
|
tiny_debug_ring_dump(STDERR_FILENO, 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
Add Box 3 (Pointer Conversion Layer) and fix POOL_TLS_PHASE1 default
## Major Changes
### 1. Box 3: Pointer Conversion Module (NEW)
- File: core/box/ptr_conversion_box.h
- Purpose: Unified BASE ↔ USER pointer conversion (single source of truth)
- API: PTR_BASE_TO_USER(), PTR_USER_TO_BASE()
- Features: Zero-overhead inline, debug mode, NULL-safe, class 7 headerless support
- Design: Header-only, fully modular, no external dependencies
### 2. POOL_TLS_PHASE1 Default OFF (CRITICAL FIX)
- File: build.sh
- Change: POOL_TLS_PHASE1 now defaults to 0 (was hardcoded to 1)
- Impact: Eliminates pthread_mutex overhead on every free() (was causing 3.3x slowdown)
- Usage: Set POOL_TLS_PHASE1=1 env var to enable if needed
### 3. Pointer Conversion Fixes (PARTIAL)
- Files: core/box/front_gate_box.c, core/tiny_alloc_fast.inc.h, etc.
- Status: Partial implementation using Box 3 API
- Note: Work in progress, some conversions still need review
### 4. Performance Investigation Report (NEW)
- File: HOTPATH_PERFORMANCE_INVESTIGATION.md
- Findings:
- Hotpath works (+24% vs baseline) after POOL_TLS fix
- Still 9.2x slower than system malloc due to:
* Heavy initialization (23.85% of cycles)
* Syscall overhead (2,382 syscalls per 100K ops)
* Workload mismatch (C7 1KB is 49.8%, but only C5 256B has hotpath)
* 9.4x more instructions than system malloc
### 5. Known Issues
- SEGV at 20K-30K iterations (pre-existing bug, not related to pointer conversions)
- Root cause: Likely active counter corruption or TLS-SLL chain issues
- Status: Under investigation
## Performance Results (100K iterations, 256B)
- Baseline (Hotpath OFF): 7.22M ops/s
- Hotpath ON: 8.98M ops/s (+24% improvement ✓)
- System malloc: 82.2M ops/s (still 9.2x faster)
## Next Steps
- P0: Fix 20K-30K SEGV bug (GDB investigation needed)
- P1: Lazy initialization (+20-25% expected)
- P1: C7 (1KB) hotpath (+30-40% expected, biggest win)
- P2: Reduce syscalls (+15-20% expected)
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-12 01:01:23 +09:00
|
|
|
|
|
|
|
|
#endif // HAKMEM_BUILD_RELEASE && !HAKMEM_DEBUG_VERBOSE
|