Fix C7 warm/TLS Release path and unify debug instrumentation

This commit is contained in:
Moe Charm (CI)
2025-12-05 23:41:01 +09:00
parent 96c2988381
commit d17ec46628
29 changed files with 1314 additions and 123 deletions

View File

@ -7,11 +7,51 @@
#define HAK_WARM_POOL_PREFILL_BOX_H
#include <stdint.h>
#include <stdatomic.h>
#include <stdio.h>
#include "../hakmem_tiny_config.h"
#include "../hakmem_tiny_superslab.h"
#include "../tiny_tls.h"
#include "../front/tiny_warm_pool.h"
#include "../box/warm_pool_stats_box.h"
#include "../box/warm_pool_rel_counters_box.h"
static inline void warm_prefill_log_c7_meta(const char* tag, TinyTLSSlab* tls) {
if (!tls || !tls->ss) return;
#if HAKMEM_BUILD_RELEASE
static _Atomic uint32_t rel_logs = 0;
uint32_t n = atomic_fetch_add_explicit(&rel_logs, 1, memory_order_relaxed);
if (n < 4) {
TinySlabMeta* meta = &tls->ss->slabs[tls->slab_idx];
fprintf(stderr,
"[REL_C7_%s] ss=%p slab=%u cls=%u used=%u cap=%u carved=%u freelist=%p\n",
tag,
(void*)tls->ss,
(unsigned)tls->slab_idx,
(unsigned)meta->class_idx,
(unsigned)meta->used,
(unsigned)meta->capacity,
(unsigned)meta->carved,
meta->freelist);
}
#else
static _Atomic uint32_t dbg_logs = 0;
uint32_t n = atomic_fetch_add_explicit(&dbg_logs, 1, memory_order_relaxed);
if (n < 4) {
TinySlabMeta* meta = &tls->ss->slabs[tls->slab_idx];
fprintf(stderr,
"[DBG_C7_%s] ss=%p slab=%u cls=%u used=%u cap=%u carved=%u freelist=%p\n",
tag,
(void*)tls->ss,
(unsigned)tls->slab_idx,
(unsigned)meta->class_idx,
(unsigned)meta->used,
(unsigned)meta->capacity,
(unsigned)meta->carved,
meta->freelist);
}
#endif
}
// Forward declarations
extern __thread TinyTLSSlab g_tls_slabs[TINY_NUM_CLASSES];
@ -45,9 +85,17 @@ extern SuperSlab* superslab_refill(int class_idx);
// Performance: Only triggered when pool is empty, cold path cost
//
static inline int warm_pool_do_prefill(int class_idx, TinyTLSSlab* tls) {
#if HAKMEM_BUILD_RELEASE
if (class_idx == 7) {
warm_pool_rel_c7_prefill_call();
}
#endif
int budget = (tiny_warm_pool_count(class_idx) == 0) ? WARM_POOL_PREFILL_BUDGET : 1;
while (budget > 0) {
if (class_idx == 7) {
warm_prefill_log_c7_meta("PREFILL_META", tls);
}
if (!tls->ss) {
// Need to load a new SuperSlab
if (!superslab_refill(class_idx)) {
@ -61,16 +109,75 @@ static inline int warm_pool_do_prefill(int class_idx, TinyTLSSlab* tls) {
break;
}
// C7 safety: prefer only pristine slabs (used=0 carved=0 freelist=NULL)
if (class_idx == 7) {
TinySlabMeta* meta = &tls->ss->slabs[tls->slab_idx];
if (meta->class_idx == 7 &&
(meta->used > 0 || meta->carved > 0 || meta->freelist != NULL)) {
#if HAKMEM_BUILD_RELEASE
static _Atomic int rel_c7_skip_logged = 0;
if (atomic_load_explicit(&rel_c7_skip_logged, memory_order_relaxed) == 0) {
fprintf(stderr,
"[REL_C7_PREFILL_SKIP_NONEMPTY] ss=%p slab=%u used=%u cap=%u carved=%u freelist=%p\n",
(void*)tls->ss,
(unsigned)tls->slab_idx,
(unsigned)meta->used,
(unsigned)meta->capacity,
(unsigned)meta->carved,
meta->freelist);
atomic_store_explicit(&rel_c7_skip_logged, 1, memory_order_relaxed);
}
#else
static __thread int dbg_c7_skip_logged = 0;
if (dbg_c7_skip_logged < 4) {
fprintf(stderr,
"[DBG_C7_PREFILL_SKIP_NONEMPTY] ss=%p slab=%u used=%u cap=%u carved=%u freelist=%p\n",
(void*)tls->ss,
(unsigned)tls->slab_idx,
(unsigned)meta->used,
(unsigned)meta->capacity,
(unsigned)meta->carved,
meta->freelist);
dbg_c7_skip_logged++;
}
#endif
tls->ss = NULL; // Drop exhausted slab and try another
budget--;
continue;
}
}
if (budget > 1) {
// Prefill mode: push to pool and load another
tiny_warm_pool_push(class_idx, tls->ss);
warm_pool_record_prefilled(class_idx);
tls->ss = NULL; // Force next iteration to refill
budget--;
} else {
// Final slab: keep in TLS for immediate carving
budget = 0;
#if HAKMEM_BUILD_RELEASE
if (class_idx == 7) {
warm_pool_rel_c7_prefill_slab();
}
#else
if (class_idx == 7) {
static __thread int dbg_c7_prefill_logs = 0;
if (dbg_c7_prefill_logs < 8) {
TinySlabMeta* meta = &tls->ss->slabs[tls->slab_idx];
fprintf(stderr,
"[DBG_C7_PREFILL] ss=%p slab=%u used=%u cap=%u carved=%u freelist=%p\n",
(void*)tls->ss,
(unsigned)tls->slab_idx,
(unsigned)meta->used,
(unsigned)meta->capacity,
(unsigned)meta->carved,
meta->freelist);
dbg_c7_prefill_logs++;
}
}
#endif
tls->ss = NULL; // Force next iteration to refill
budget--;
} else {
// Final slab: keep in TLS for immediate carving
budget = 0;
}
}
return 0; // Success