Files
hakmem/core/smallobject_policy_v7.c

119 lines
4.0 KiB
C
Raw Normal View History

// smallobject_policy_v7.c - Policy Box implementation
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "box/smallobject_policy_v7_box.h"
#ifndef likely
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
#endif
// ============================================================================
// TLS Policy Snapshot
// ============================================================================
static __thread SmallPolicyV7 g_small_policy_v7;
static __thread int g_small_policy_v7_init = 0;
const SmallPolicyV7* small_policy_v7_snapshot(void) {
if (unlikely(!g_small_policy_v7_init)) {
small_policy_v7_init_from_env(&g_small_policy_v7);
g_small_policy_v7_init = 1;
}
return &g_small_policy_v7;
}
// ============================================================================
// ENV Helpers
// ============================================================================
static inline bool env_enabled(const char* name) {
const char* e = getenv(name);
return (e && *e && *e != '0');
}
static inline uint32_t env_class_mask(const char* name, uint32_t default_mask) {
const char* e = getenv(name);
if (e && *e) {
return (uint32_t)strtoul(e, NULL, 0);
}
return default_mask;
}
// ============================================================================
// Policy Initialization from ENV
// ============================================================================
void small_policy_v7_init_from_env(SmallPolicyV7* policy) {
if (!policy) return;
// Default: all classes go to LEGACY
for (int i = 0; i < 8; i++) {
policy->route_kind[i] = SMALL_ROUTE_LEGACY;
}
// Priority 3: MID_v3 (257-768B, C5-C6 range)
// ENV: HAKMEM_MID_V3_ENABLED, HAKMEM_MID_V3_CLASSES
if (env_enabled("HAKMEM_MID_V3_ENABLED")) {
uint32_t mid_mask = env_class_mask("HAKMEM_MID_V3_CLASSES", 0x60); // C5-C6 default
for (int i = 0; i < 8; i++) {
if (mid_mask & (1u << i)) {
policy->route_kind[i] = SMALL_ROUTE_MID_V3;
}
}
}
// Priority 2: SmallObject v7 (research box, C6-only for now)
// ENV: HAKMEM_SMALL_HEAP_V7_ENABLED, HAKMEM_SMALL_HEAP_V7_CLASSES
if (env_enabled("HAKMEM_SMALL_HEAP_V7_ENABLED")) {
uint32_t v7_mask = env_class_mask("HAKMEM_SMALL_HEAP_V7_CLASSES", 0x40); // C6 default
for (int i = 0; i < 8; i++) {
if (v7_mask & (1u << i)) {
policy->route_kind[i] = SMALL_ROUTE_V7;
}
}
}
// Priority 1: ULTRA (highest priority, C4-C7)
// ENV: HAKMEM_TINY_C7_ULTRA_ENABLED (C7), HAKMEM_TINY_C6_ULTRA_FREE_ENABLED (C6), etc.
// Note: For now, we check individual ULTRA ENV vars
// C7 ULTRA (default ON)
if (env_enabled("HAKMEM_TINY_C7_ULTRA_ENABLED")) {
policy->route_kind[7] = SMALL_ROUTE_ULTRA;
}
// C6 ULTRA (if C6 ULTRA free is enabled, route to ULTRA)
// Note: This is a free-only optimization, not full ULTRA for C6 yet
// Keep C6 routing as-is (v7 or MID_v3) for now
// C4-C5 ULTRA (if enabled via ENV)
// TODO: Add HAKMEM_TINY_C4_ULTRA_ENABLED / C5 when implemented
// Debug output (if needed)
static int g_debug_once = 0;
if (!g_debug_once) {
g_debug_once = 1;
fprintf(stderr, "[POLICY_V7_INIT] Route assignments:\n");
for (int i = 0; i < 8; i++) {
fprintf(stderr, " C%d: %s\n", i, small_route_kind_name(policy->route_kind[i]));
}
}
}
// ============================================================================
// Utility
// ============================================================================
const char* small_route_kind_name(SmallRouteKind kind) {
switch (kind) {
case SMALL_ROUTE_ULTRA: return "ULTRA";
case SMALL_ROUTE_V7: return "V7";
case SMALL_ROUTE_MID_V3: return "MID_V3";
case SMALL_ROUTE_LEGACY: return "LEGACY";
default: return "UNKNOWN";
}
}