Priority-2: ENV Cache - Warm Path (FastCache/SuperSlab) getenv() 置換

変更内容:
- hakmem_env_cache.h: 2つの新ENV変数を追加
  (TINY_FAST_STATS, TINY_UNIFIED_CACHE)
- tiny_fastcache.c: 2箇所の getenv() を置換
  (TINY_PROFILE, TINY_FAST_STATS)
- tiny_fastcache.h: 1箇所の getenv() を置換
  (TINY_PROFILE in inline function)
- superslab_slab.c: 1箇所の getenv() を置換
  (TINY_SLL_DIAG)
- tiny_unified_cache.c: 1箇所の getenv() を置換
  (TINY_UNIFIED_CACHE)

効果: Warm path層からも syscall を排除 (ENV変数数: 28→30)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Moe Charm (CI)
2025-12-02 20:25:48 +09:00
parent 8336febdcb
commit 936dc365ba
5 changed files with 31 additions and 25 deletions

View File

@ -8,6 +8,7 @@
#include "../superslab/superslab_inline.h" // Phase 23-E: ss_active_add, slab_index_for, ss_slabs_capacity #include "../superslab/superslab_inline.h" // Phase 23-E: ss_active_add, slab_index_for, ss_slabs_capacity
#include "../hakmem_super_registry.h" // For hak_super_lookup (pointer→SuperSlab) #include "../hakmem_super_registry.h" // For hak_super_lookup (pointer→SuperSlab)
#include "../box/pagefault_telemetry_box.h" // Phase 24: Box PageFaultTelemetry (Tiny page touch stats) #include "../box/pagefault_telemetry_box.h" // Phase 24: Box PageFaultTelemetry (Tiny page touch stats)
#include "../hakmem_env_cache.h" // Priority-2: ENV cache (eliminate syscalls)
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -37,10 +38,10 @@ __thread uint64_t g_unified_cache_full[TINY_NUM_CLASSES] = {0};
// Enable flag (default: ON, disable with HAKMEM_TINY_UNIFIED_CACHE=0) // Enable flag (default: ON, disable with HAKMEM_TINY_UNIFIED_CACHE=0)
int unified_cache_enabled(void) { int unified_cache_enabled(void) {
// Priority-2: Use cached ENV (eliminate lazy-init static overhead)
static int g_enable = -1; static int g_enable = -1;
if (__builtin_expect(g_enable == -1, 0)) { if (__builtin_expect(g_enable == -1, 0)) {
const char* e = getenv("HAKMEM_TINY_UNIFIED_CACHE"); g_enable = HAK_ENV_TINY_UNIFIED_CACHE();
g_enable = (e && *e && *e == '0') ? 0 : 1; // default ON
#if !HAKMEM_BUILD_RELEASE #if !HAKMEM_BUILD_RELEASE
if (g_enable) { if (g_enable) {
fprintf(stderr, "[Unified-INIT] unified_cache_enabled() = %d\n", g_enable); fprintf(stderr, "[Unified-INIT] unified_cache_enabled() = %d\n", g_enable);

View File

@ -49,10 +49,12 @@ typedef struct {
// ===== Warm Path: Lazy Init (1 variable) ===== // ===== Warm Path: Lazy Init (1 variable) =====
int ss_map_trace; // HAKMEM_SS_MAP_TRACE (default: 0) int ss_map_trace; // HAKMEM_SS_MAP_TRACE (default: 0)
// ===== Warm Path: FastCache (3 variables) ===== // ===== Warm Path: FastCache (5 variables) =====
int tiny_fast_debug; // HAKMEM_TINY_FAST_DEBUG (default: 0) int tiny_fast_debug; // HAKMEM_TINY_FAST_DEBUG (default: 0)
int tiny_fast_debug_max; // HAKMEM_TINY_FAST_DEBUG_MAX (default: 0) int tiny_fast_debug_max; // HAKMEM_TINY_FAST_DEBUG_MAX (default: 0)
int tiny_front_direct; // HAKMEM_TINY_FRONT_DIRECT (default: 0) int tiny_front_direct; // HAKMEM_TINY_FRONT_DIRECT (default: 0)
int tiny_fast_stats; // HAKMEM_TINY_FAST_STATS (default: 0)
int tiny_unified_cache; // HAKMEM_TINY_UNIFIED_CACHE (default: 1)
// ===== Cold Path: Headers/Debug (5 variables) ===== // ===== Cold Path: Headers/Debug (5 variables) =====
int tiny_restore_header; // HAKMEM_TINY_RESTORE_HEADER (default: 0) int tiny_restore_header; // HAKMEM_TINY_RESTORE_HEADER (default: 0)
@ -155,6 +157,13 @@ static inline void hakmem_env_cache_init(void) {
e = getenv("HAKMEM_TINY_FRONT_DIRECT"); e = getenv("HAKMEM_TINY_FRONT_DIRECT");
g_hak_env_cache.tiny_front_direct = (e && *e && *e != '0') ? 1 : 0; g_hak_env_cache.tiny_front_direct = (e && *e && *e != '0') ? 1 : 0;
e = getenv("HAKMEM_TINY_FAST_STATS");
g_hak_env_cache.tiny_fast_stats = (e && *e && *e != '0') ? 1 : 0;
e = getenv("HAKMEM_TINY_UNIFIED_CACHE");
// Default: 1 (enabled), set HAKMEM_TINY_UNIFIED_CACHE=0 to disable
g_hak_env_cache.tiny_unified_cache = (e && *e && *e == '0') ? 0 : 1;
} }
// ===== Cold Path: Headers/Debug ===== // ===== Cold Path: Headers/Debug =====
@ -180,7 +189,7 @@ static inline void hakmem_env_cache_init(void) {
#if !HAKMEM_BUILD_RELEASE #if !HAKMEM_BUILD_RELEASE
// Debug: Print cache summary (stderr only) // Debug: Print cache summary (stderr only)
if (!g_hak_env_cache.quiet) { if (!g_hak_env_cache.quiet) {
fprintf(stderr, "[ENV_CACHE_INIT] Parsed %d ENV variables at startup\n", 28); fprintf(stderr, "[ENV_CACHE_INIT] Parsed %d ENV variables at startup\n", 30);
fprintf(stderr, "[ENV_CACHE_INIT] Hot path syscalls eliminated: ~2000/sec → 0/sec\n"); fprintf(stderr, "[ENV_CACHE_INIT] Hot path syscalls eliminated: ~2000/sec → 0/sec\n");
fflush(stderr); fflush(stderr);
} }
@ -212,6 +221,8 @@ static inline void hakmem_env_cache_init(void) {
#define HAK_ENV_TINY_FAST_DEBUG() (g_hak_env_cache.tiny_fast_debug) #define HAK_ENV_TINY_FAST_DEBUG() (g_hak_env_cache.tiny_fast_debug)
#define HAK_ENV_TINY_FAST_DEBUG_MAX() (g_hak_env_cache.tiny_fast_debug_max) #define HAK_ENV_TINY_FAST_DEBUG_MAX() (g_hak_env_cache.tiny_fast_debug_max)
#define HAK_ENV_TINY_FRONT_DIRECT() (g_hak_env_cache.tiny_front_direct) #define HAK_ENV_TINY_FRONT_DIRECT() (g_hak_env_cache.tiny_front_direct)
#define HAK_ENV_TINY_FAST_STATS() (g_hak_env_cache.tiny_fast_stats)
#define HAK_ENV_TINY_UNIFIED_CACHE() (g_hak_env_cache.tiny_unified_cache)
// Cold path accessors // Cold path accessors
#define HAK_ENV_TINY_RESTORE_HEADER() (g_hak_env_cache.tiny_restore_header) #define HAK_ENV_TINY_RESTORE_HEADER() (g_hak_env_cache.tiny_restore_header)

View File

@ -5,6 +5,7 @@
#include "hakmem_tiny_superslab_internal.h" #include "hakmem_tiny_superslab_internal.h"
#include "box/slab_recycling_box.h" #include "box/slab_recycling_box.h"
#include "hakmem_env_cache.h" // Priority-2: ENV cache (eliminate syscalls)
// ============================================================================ // ============================================================================
// Remote Drain (MPSC queue to freelist conversion) // Remote Drain (MPSC queue to freelist conversion)
@ -42,16 +43,14 @@ void _ss_remote_drain_to_freelist_unsafe(SuperSlab* ss, int slab_idx, TinySlabMe
uintptr_t cur = head; uintptr_t cur = head;
while (cur != 0) { while (cur != 0) {
uintptr_t next = *(uintptr_t*)cur; // remote-next stored at offset 0 uintptr_t next = *(uintptr_t*)cur; // remote-next stored at offset 0
// Priority-2: Use cached ENV (eliminate lazy-init static overhead)
if (__builtin_expect(g_remote_drain_diag_en == -1, 0)) {
#if !HAKMEM_BUILD_RELEASE #if !HAKMEM_BUILD_RELEASE
if (__builtin_expect(g_remote_drain_diag_en == -1, 0)) { g_remote_drain_diag_en = HAK_ENV_TINY_SLL_DIAG();
const char* e = getenv("HAKMEM_TINY_SLL_DIAG");
g_remote_drain_diag_en = (e && *e && *e != '0') ? 1 : 0;
}
#else #else
if (__builtin_expect(g_remote_drain_diag_en == -1, 0)) {
g_remote_drain_diag_en = 0; g_remote_drain_diag_en = 0;
}
#endif #endif
}
if (__builtin_expect(g_remote_drain_diag_en, 0)) { if (__builtin_expect(g_remote_drain_diag_en, 0)) {
uintptr_t addr = (uintptr_t)next; uintptr_t addr = (uintptr_t)next;
if (addr != 0 && (addr < 4096 || addr > 0x00007fffffffffffULL)) { if (addr != 0 && (addr < 4096 || addr > 0x00007fffffffffffULL)) {

View File

@ -5,6 +5,7 @@
#include "hakmem_tiny.h" #include "hakmem_tiny.h"
#include "hakmem_tiny_superslab.h" #include "hakmem_tiny_superslab.h"
#include "box/tiny_next_ptr_box.h" // Phase E1-CORRECT: Box API #include "box/tiny_next_ptr_box.h" // Phase E1-CORRECT: Box API
#include "hakmem_env_cache.h" // Priority-2: ENV cache (eliminate syscalls)
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -76,9 +77,9 @@ static __thread uint64_t g_refill_total_blocks = 0; // Total blocks actually al
int g_profile_enabled = -1; // -1: uninitialized, 0: off, 1: on (extern in header) int g_profile_enabled = -1; // -1: uninitialized, 0: off, 1: on (extern in header)
static inline int profile_enabled(void) { static inline int profile_enabled(void) {
// Priority-2: Use cached ENV (eliminate lazy-init syscall overhead)
if (__builtin_expect(g_profile_enabled == -1, 0)) { if (__builtin_expect(g_profile_enabled == -1, 0)) {
const char* env = getenv("HAKMEM_TINY_PROFILE"); g_profile_enabled = HAK_ENV_TINY_PROFILE();
g_profile_enabled = (env && *env && *env != '0') ? 1 : 0;
} }
return g_profile_enabled; return g_profile_enabled;
} }
@ -204,15 +205,8 @@ void tiny_fast_drain(int class_idx) {
// ========== Debug Stats ========== // ========== Debug Stats ==========
void tiny_fast_print_stats(void) { void tiny_fast_print_stats(void) {
static const char* env = NULL; // Priority-2: Use cached ENV (eliminate static lazy-init overhead)
static int checked = 0; if (HAK_ENV_TINY_FAST_STATS()) {
if (!checked) {
env = getenv("HAKMEM_TINY_FAST_STATS");
checked = 1;
}
if (env && *env && *env != '0') {
fprintf(stderr, "[TINY_FAST] refills=%lu drains=%lu\n", fprintf(stderr, "[TINY_FAST] refills=%lu drains=%lu\n",
(unsigned long)g_tiny_fast_refill_count, (unsigned long)g_tiny_fast_refill_count,
(unsigned long)g_tiny_fast_drain_count); (unsigned long)g_tiny_fast_drain_count);

View File

@ -6,8 +6,9 @@
#include <stdint.h> #include <stdint.h>
#include <stddef.h> #include <stddef.h>
#include <string.h> #include <string.h>
#include <stdlib.h> // For getenv() #include <stdlib.h>
#include "box/tiny_next_ptr_box.h" // Box API: Next pointer read/write #include "hakmem_env_cache.h" // Priority-2: ENV cache (eliminate syscalls)
#include "box/tiny_next_ptr_box.h" // Box API: Next pointer read/write
// ========== Configuration ========== // ========== Configuration ==========
@ -68,10 +69,10 @@ static inline uint64_t tiny_fast_rdtsc(void) { return 0; }
extern int g_profile_enabled; extern int g_profile_enabled;
static inline int tiny_fast_profile_enabled(void) { static inline int tiny_fast_profile_enabled(void) {
#if !HAKMEM_BUILD_RELEASE #if !HAKMEM_BUILD_RELEASE
// Priority-2: Use cached ENV (eliminate lazy-init inline overhead)
extern int g_profile_enabled; extern int g_profile_enabled;
if (__builtin_expect(g_profile_enabled == -1, 0)) { if (__builtin_expect(g_profile_enabled == -1, 0)) {
const char* env = getenv("HAKMEM_TINY_PROFILE"); g_profile_enabled = HAK_ENV_TINY_PROFILE();
g_profile_enabled = (env && *env && *env != '0') ? 1 : 0;
} }
return g_profile_enabled; return g_profile_enabled;
#else #else