Boxify superslab registry, add bench profile, and document C7 hotpath experiments

This commit is contained in:
Moe Charm (CI)
2025-12-07 03:12:27 +09:00
parent 18faa6a1c4
commit fda6cd2e67
71 changed files with 2052 additions and 286 deletions

View File

@ -17,6 +17,7 @@
#undef WARM_POOL_REL_DEFINE
#include "../box/c7_meta_used_counter_box.h" // Box: C7 meta->used increment counters
#include "../box/warm_pool_prefill_box.h" // Box: Warm Pool Prefill (secondary optimization)
#include "../box/tiny_mem_stats_box.h" // Box: Tiny front memory accounting
#include "../hakmem_env_cache.h" // Priority-2: ENV cache (eliminate syscalls)
#include "../box/tiny_page_box.h" // Tiny-Plus Page Box (C5C7 initial hook)
#include "../box/ss_tls_bind_box.h" // Box: TLS Bind (SuperSlab -> TLS binding)
@ -205,6 +206,8 @@ void unified_cache_init(void) {
continue; // Skip this class, try others
}
tiny_mem_stats_add_unified((ssize_t)(cap * sizeof(void*)));
g_unified_cache[cls].capacity = (uint16_t)cap;
g_unified_cache[cls].mask = (uint16_t)(cap - 1);
g_unified_cache[cls].head = 0;
@ -522,6 +525,7 @@ hak_base_ptr_t unified_cache_refill(int class_idx) {
int warm_enabled = policy ? policy->warm_enabled : 0;
int warm_cap = policy ? policy->warm_cap : 0;
int page_enabled = policy ? policy->page_box_enabled : 0;
TinyTLSSlab* tls = &g_tls_slabs[class_idx];
// ✅ Phase 11+: Ensure cache is initialized (lazy init for cold path)
if (!cache->slots) {
@ -562,12 +566,15 @@ hak_base_ptr_t unified_cache_refill(int class_idx) {
void* out[512];
int produced = 0;
int tls_carved = 0; // Debug bookkeeping: track TLS carve experiment hits
#if HAKMEM_BUILD_RELEASE
(void)tls_carved;
#endif
// ========== PAGE BOX HOT PATHTiny-Plus 層): Try page box FIRST ==========
// 将来的に C7 専用の page-level freelist 管理をここに統合する。
// いまは stub 実装で常に 0 を返すが、Box 境界としての接続だけ先に行う。
if (page_enabled && tiny_page_box_is_enabled(class_idx)) {
int page_produced = tiny_page_box_refill(class_idx, out, room);
int page_produced = tiny_page_box_refill(class_idx, tls, out, room);
if (page_produced > 0) {
// Store blocks into cache and return first
void* first = out[0];
@ -625,45 +632,58 @@ hak_base_ptr_t unified_cache_refill(int class_idx) {
#endif
SuperSlab* warm_ss = tiny_warm_pool_pop(class_idx);
if (warm_ss) {
int allow_tls_bind = policy && policy->tls_carve_enabled;
int allow_tls_carve = allow_tls_bind;
int warm_mode = 0;
if (class_idx == 7) {
#if !HAKMEM_BUILD_RELEASE
warm_pool_dbg_c7_hit();
#endif
int warm_mode = warm_tls_bind_mode_c7();
if (warm_mode >= 1) {
int cap = ss_slabs_capacity(warm_ss);
int slab_idx = -1;
warm_mode = warm_tls_bind_mode_c7();
allow_tls_bind = (warm_mode >= 1);
allow_tls_carve = (warm_mode == 2);
}
// Simple heuristic: first slab matching class
for (int i = 0; i < cap; i++) {
if (tiny_get_class_from_ss(warm_ss, i) == class_idx) {
slab_idx = i;
break;
}
if (allow_tls_bind) {
int cap = ss_slabs_capacity(warm_ss);
int slab_idx = -1;
// Simple heuristic: first slab matching class
for (int i = 0; i < cap; i++) {
if (tiny_get_class_from_ss(warm_ss, i) == class_idx) {
slab_idx = i;
break;
}
}
if (slab_idx >= 0) {
TinyTLSSlab* tls = &g_tls_slabs[class_idx];
uint32_t tid = (uint32_t)(uintptr_t)pthread_self();
if (ss_tls_bind_one(class_idx, tls, warm_ss, slab_idx, tid)) {
if (slab_idx >= 0) {
uint32_t tid = (uint32_t)(uintptr_t)pthread_self();
if (ss_tls_bind_one(class_idx, tls, warm_ss, slab_idx, tid)) {
if (class_idx == 7) {
warm_tls_bind_log_success(warm_ss, slab_idx);
}
// Mode 2: carve a single block via TLS fast path
if (warm_mode == 2) {
#if !HAKMEM_BUILD_RELEASE
// Mode 2: carve a single block via TLS fast path (policy enabled classes)
if (allow_tls_carve) {
#if !HAKMEM_BUILD_RELEASE
if (class_idx == 7) {
warm_pool_dbg_c7_tls_attempt();
#endif
TinyTLSCarveOneResult tls_carve =
tiny_tls_carve_one_block(tls, class_idx);
if (tls_carve.block) {
}
#endif
TinyTLSCarveOneResult tls_carve =
tiny_tls_carve_one_block(tls, class_idx);
if (tls_carve.block) {
if (class_idx == 7) {
warm_tls_bind_log_tls_carve(warm_ss, slab_idx, tls_carve.block);
#if !HAKMEM_BUILD_RELEASE
warm_pool_dbg_c7_tls_success();
#endif
out[0] = tls_carve.block;
produced = 1;
tls_carved = 1;
} else {
}
out[0] = tls_carve.block;
produced = 1;
tls_carved = 1;
} else {
if (class_idx == 7) {
warm_tls_bind_log_tls_fail(warm_ss, slab_idx);
#if !HAKMEM_BUILD_RELEASE
warm_pool_dbg_c7_tls_fail();
@ -774,8 +794,6 @@ hak_base_ptr_t unified_cache_refill(int class_idx) {
warm_pool_record_miss(class_idx);
}
TinyTLSSlab* tls = &g_tls_slabs[class_idx];
// Step 1: Ensure SuperSlab available via normal refill
// Enhanced: Use Warm Pool Prefill Box for secondary prefill when pool is empty
if (warm_enabled) {