diff --git a/core/pool_tls.c b/core/pool_tls.c index c85c215e..baebb400 100644 --- a/core/pool_tls.c +++ b/core/pool_tls.c @@ -6,10 +6,15 @@ #include #include #include "pool_tls_registry.h" - +#ifdef HAKMEM_POOL_TLS_BIND_BOX +#include "pool_tls_bind.h" +#else +// gettid_cached is defined in pool_tls_bind.h when BIND_BOX is enabled static inline pid_t gettid_cached(void){ static __thread pid_t t=0; if (__builtin_expect(t==0,0)) t=(pid_t)syscall(SYS_gettid); return t; } +#endif + #include // Class sizes: 8KB, 16KB, 24KB, 32KB, 40KB, 48KB, 52KB @@ -146,15 +151,30 @@ void pool_free(void* ptr) { // Need registry lookup (slower fallback) - not implemented in Phase 1 return; #endif + // Owner resolution via page registry - pid_t owner_tid=0; int reg_cls=-1; - if (pool_reg_lookup(ptr, &owner_tid, ®_cls)){ - pid_t me = gettid_cached(); - if (owner_tid != me){ + pid_t owner_tid = 0; + int reg_cls = -1; + if (pool_reg_lookup(ptr, &owner_tid, ®_cls)) { +#ifdef HAKMEM_POOL_TLS_BIND_BOX + // POOL_TLS_BIND_BOX: Fast TID comparison (no repeated gettid syscalls) + if (!pool_tls_is_mine_tid(owner_tid)) { + // Cross-thread free extern int pool_remote_push(int class_idx, void* ptr, int owner_tid); (void)pool_remote_push(class_idx, ptr, owner_tid); return; } + // Same-thread: Continue to fast free path below +#else + // Original gettid comparison + pid_t me = gettid_cached(); + if (owner_tid != me) { + // Cross-thread free + extern int pool_remote_push(int class_idx, void* ptr, int owner_tid); + (void)pool_remote_push(class_idx, ptr, owner_tid); + return; + } +#endif } // Same-thread: Push to TLS freelist (2-3 instructions) diff --git a/core/pool_tls_bind.c b/core/pool_tls_bind.c new file mode 100644 index 00000000..cdba4768 --- /dev/null +++ b/core/pool_tls_bind.c @@ -0,0 +1,11 @@ +#include "pool_tls_bind.h" + +/** + * POOL_TLS_BIND_BOX - TID Cache Implementation + * + * This file provides the TLS storage for thread ID caching. + * All logic is inlined in pool_tls_bind.h for performance. + */ + +// TLS storage (per-thread, automatically zero-initialized) +__thread PoolTLSBind g_pool_tls_bind = {0}; diff --git a/core/pool_tls_bind.h b/core/pool_tls_bind.h new file mode 100644 index 00000000..b08258fc --- /dev/null +++ b/core/pool_tls_bind.h @@ -0,0 +1,56 @@ +#ifndef HAKMEM_POOL_TLS_BIND_H +#define HAKMEM_POOL_TLS_BIND_H + +#include +#include +#include +#include + +/** + * POOL_TLS_BIND_BOX - TID Cache for Fast Same-Thread Detection + * + * Box Theory: + * - Boundary: Thread initialization - cache TID in TLS once + * - Internal: Hot path uses TID comparison (no gettid syscall) + * - Fallback: gettid_cached() on first access + * + * Performance: + * - Eliminates repeated gettid() calls from hot path + * - Simple TID comparison (1 comparison vs registry lookup) + * - Expected: Reduce cache misses and syscall overhead + */ + +// TLS binding for fast TID caching +typedef struct PoolTLSBind { + pid_t tid; // My thread ID (cached, 0 = uninitialized) +} PoolTLSBind; + +// TLS cache (per-thread, automatically zero-initialized) +extern __thread PoolTLSBind g_pool_tls_bind; + +// Inline helper: gettid with caching +static inline pid_t gettid_cached(void) { + static __thread pid_t cached_tid = 0; + if (__builtin_expect(cached_tid == 0, 0)) { + cached_tid = (pid_t)syscall(SYS_gettid); + } + return cached_tid; +} + +// API + +// Get my thread ID (cached in TLS) +static inline pid_t pool_get_my_tid(void) { + if (__builtin_expect(g_pool_tls_bind.tid == 0, 0)) { + g_pool_tls_bind.tid = gettid_cached(); + } + return g_pool_tls_bind.tid; +} + +// Fast same-thread check (TID comparison only) +// Returns 1 if owner_tid matches my TID, 0 otherwise +static inline int pool_tls_is_mine_tid(pid_t owner_tid) { + return owner_tid == pool_get_my_tid(); +} + +#endif // HAKMEM_POOL_TLS_BIND_H