P-Tier + Tiny Route Policy: Aggressive Superslab Management + Safe Routing

## Phase 1: Utilization-Aware Superslab Tiering (案B実装済)

- Add ss_tier_box.h: Classify SuperSlabs into HOT/DRAINING/FREE based on utilization
  - HOT (>25%): Accept new allocations
  - DRAINING (≤25%): Drain only, no new allocs
  - FREE (0%): Ready for eager munmap

- Enhanced shared_pool_release_slab():
  - Check tier transition after each slab release
  - If tier→FREE: Force remaining slots to EMPTY and call superslab_free() immediately
  - Bypasses LRU cache to prevent registry bloat from accumulating DRAINING SuperSlabs

- Test results (bench_random_mixed_hakmem):
  - 1M iterations:  ~1.03M ops/s (previously passed)
  - 10M iterations:  ~1.15M ops/s (previously: registry full error)
  - 50M iterations:  ~1.08M ops/s (stress test)

## Phase 2: Tiny Front Routing Policy (新規Box)

- Add tiny_route_box.h/c: Single 8-byte table for class→routing decisions
  - ROUTE_TINY_ONLY: Tiny front exclusive (no fallback)
  - ROUTE_TINY_FIRST: Try Tiny, fallback to Pool if fails
  - ROUTE_POOL_ONLY: Skip Tiny entirely

- Profiles via HAKMEM_TINY_PROFILE ENV:
  - "hot": C0-C3=TINY_ONLY, C4-C6=TINY_FIRST, C7=POOL_ONLY
  - "conservative" (default): All TINY_FIRST
  - "off": All POOL_ONLY (disable Tiny)
  - "full": All TINY_ONLY (microbench mode)

- A/B test results (ws=256, 100k ops random_mixed):
  - Default (conservative): ~2.90M ops/s
  - hot: ~2.65M ops/s (more conservative)
  - off: ~2.86M ops/s
  - full: ~2.98M ops/s (slightly best)

## Design Rationale

### Registry Pressure Fix (案B)
- Problem: DRAINING tier SS occupied registry indefinitely
- Solution: When total_active_blocks→0, immediately free to clear registry slot
- Result: No more "registry full" errors under stress

### Routing Policy Box (新)
- Problem: Tiny front optimization scattered across ENV/branches
- Solution: Centralize routing in single table, select profiles via ENV
- Benefit: Safe A/B testing without touching hot path code
- Future: Integrate with RSS budget/learning layers for dynamic profile switching

## Next Steps (性能最適化)
- Profile Tiny front internals (TLS SLL, FastCache, Superslab backend latency)
- Identify bottleneck between current ~2.9M ops/s and mimalloc ~100M ops/s
- Consider:
  - Reduce shared pool lock contention
  - Optimize unified cache hit rate
  - Streamline Superslab carving logic

🤖 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-04 18:01:25 +09:00
parent 984cca41ef
commit d5e6ed535c
13 changed files with 647 additions and 25 deletions

50
core/box/tiny_route_box.h Normal file
View File

@ -0,0 +1,50 @@
// tiny_route_box.h - Box: Tiny Front Routing Policy
//
// Purpose:
// Decide, per Tiny class, whether allocation should go through Tiny front
// or directly to the Pool/backend. This keeps routing policy in a single,
// cheap table lookup, without getenv() or complex logic in the hot path.
//
// Box Theory:
// - Single Responsibility:
// Only decides "Tiny vs Pool vs Tiny+Fallback" per class.
// - Clear Boundary:
// Front Gate / Alloc Gatekeeper calls tiny_route_get(class_idx) once.
// Tiny Fast Path and Pool backend remain unchanged.
// - Reversible / A/B:
// Profiles are selected via HAKMEM_TINY_PROFILE ENV at init time.
// Hot path is stable; routing can be tuned without touching fast code.
#ifndef TINY_ROUTE_BOX_H
#define TINY_ROUTE_BOX_H
#include <stdint.h>
// Routing policy per Tiny class.
typedef enum {
ROUTE_TINY_ONLY = 0, // Tiny front only (no fallback; failure bubbles up)
ROUTE_TINY_FIRST = 1, // Try Tiny front, then fallback to Pool backend
ROUTE_POOL_ONLY = 2, // Skip Tiny entirely, use Pool/backend only
} TinyRoutePolicy;
// Global routing table for Tiny classes (0..7).
// Initialized once from ENV: HAKMEM_TINY_PROFILE.
extern uint8_t g_tiny_route[8];
// Initialize routing table from ENV profile.
// Profiles:
// "hot" C0-C3=TINY_ONLY, C4-C6=TINY_FIRST, C7=POOL_ONLY
// "conservative" 全クラス TINY_FIRSTデフォルト
// "off" 全クラス POOL_ONLYTiny 無効)
// "full" 全クラス TINY_ONLYmicrobench 用)
void tiny_route_init(void);
// Hot path helper: return routing policy for a given class.
// Uses simple array lookup; class_idx is masked to [0,7] defensively.
static inline TinyRoutePolicy tiny_route_get(int class_idx)
{
return (TinyRoutePolicy)g_tiny_route[class_idx & 7];
}
#endif // TINY_ROUTE_BOX_H