Files
hakmem/core/smallobject_policy_v7.c
Moe Charm (CI) 8143e8b797 Phase v7-4: Policy Box 導入 (L3 層の明確化とフロント芯の作り直し)
- SmallPolicyV7 Box: L3 Policy layer に配置、route 決定を一元化
- Route kind enum: SMALL_ROUTE_ULTRA / V7 / MID_V3 / LEGACY
- ENV priority (fixed): ULTRA > v7 > MID_v3 > LEGACY
- Frontend integration: v7 routing を Policy Box 経由に変更 (段階移行)
- Legacy compatibility: 既存の tiny_route_env_box.h は併用維持

Box Theory layer structure:
- L0: ULTRA (C4-C7, FROZEN)
- L1: SmallObject v7 (research box)
- L1': MID_v3 / LEGACY (fallback)
- L2: Segment / RegionId
- L3: Policy / Stats / Learner ← Policy Box added here

Frontend now follows clean "size→class→route_kind→switch" pattern.
ENV variables read once at Policy init, not scattered across frontend.

Future: ULTRA/MID_v3/LEGACY consolidation, Learner integration, flexible priority.

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

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2025-12-12 03:50:58 +09:00

119 lines
4.0 KiB
C

// 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";
}
}