2025-11-05 12:31:14 +09:00
|
|
|
// hakmem_tiny_superslab.h - SuperSlab allocator for Tiny Pool (Phase 6.22)
|
|
|
|
|
// Purpose: mimalloc-inspired 2MB aligned slab allocation for fast pointer→slab lookup
|
|
|
|
|
// License: MIT
|
|
|
|
|
// Date: 2025-10-24
|
2025-11-07 23:05:33 +09:00
|
|
|
// Phase 6-2.8: Refactored into modular headers (types, inline)
|
2025-11-05 12:31:14 +09:00
|
|
|
|
|
|
|
|
#ifndef HAKMEM_TINY_SUPERSLAB_H
|
|
|
|
|
#define HAKMEM_TINY_SUPERSLAB_H
|
|
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
#include <stddef.h>
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
#include <stdatomic.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <time.h> // Phase 8.3: For clock_gettime() in hak_now_ns()
|
|
|
|
|
#include <signal.h>
|
|
|
|
|
#include <stdio.h> // For fprintf() debugging
|
|
|
|
|
#include <pthread.h>
|
2025-11-07 23:05:33 +09:00
|
|
|
|
|
|
|
|
// Phase 6-2.8: Modular headers (types, inline functions)
|
|
|
|
|
#include "superslab/superslab_types.h"
|
|
|
|
|
#include "superslab/superslab_inline.h"
|
|
|
|
|
|
|
|
|
|
// Legacy includes (for backward compatibility)
|
2025-11-05 12:31:14 +09:00
|
|
|
#include "tiny_debug_ring.h"
|
|
|
|
|
#include "tiny_remote.h"
|
2025-11-07 21:45:20 +09:00
|
|
|
#include "hakmem_tiny_superslab_constants.h" // Phase 6-2.5: Centralized layout constants
|
2025-11-05 12:31:14 +09:00
|
|
|
|
|
|
|
|
// Debug instrumentation flags (defined in hakmem_tiny.c)
|
|
|
|
|
extern int g_debug_remote_guard;
|
|
|
|
|
extern int g_tiny_safe_free_strict;
|
2025-11-07 01:27:04 +09:00
|
|
|
extern _Atomic uint64_t g_ss_active_dec_calls;
|
|
|
|
|
|
2025-11-07 23:05:33 +09:00
|
|
|
uint32_t tiny_remote_drain_threshold(void);
|
2025-11-05 12:31:14 +09:00
|
|
|
|
|
|
|
|
// ============================================================================
|
|
|
|
|
// SuperSlab Management Functions
|
|
|
|
|
// ============================================================================
|
|
|
|
|
|
|
|
|
|
// Allocate a new SuperSlab (2MB aligned)
|
|
|
|
|
SuperSlab* superslab_allocate(uint8_t size_class);
|
|
|
|
|
|
|
|
|
|
// Free a SuperSlab
|
|
|
|
|
void superslab_free(SuperSlab* ss);
|
|
|
|
|
|
|
|
|
|
// Initialize a slab within SuperSlab
|
|
|
|
|
void superslab_init_slab(SuperSlab* ss, int slab_idx, size_t block_size, uint32_t owner_tid);
|
|
|
|
|
|
|
|
|
|
// Mark a slab as active
|
|
|
|
|
void superslab_activate_slab(SuperSlab* ss, int slab_idx);
|
|
|
|
|
|
|
|
|
|
// Mark a slab as inactive
|
|
|
|
|
void superslab_deactivate_slab(SuperSlab* ss, int slab_idx);
|
|
|
|
|
|
|
|
|
|
// Find first free slab index (-1 if none)
|
|
|
|
|
int superslab_find_free_slab(SuperSlab* ss);
|
|
|
|
|
|
|
|
|
|
// Statistics
|
|
|
|
|
void superslab_print_stats(SuperSlab* ss);
|
|
|
|
|
|
|
|
|
|
// Phase 8.3: ACE statistics
|
|
|
|
|
void superslab_ace_print_stats(void);
|
|
|
|
|
|
|
|
|
|
// ============================================================================
|
|
|
|
|
// Phase 8.3: ACE (Adaptive Cache Engine) - SuperSlab adaptive sizing
|
|
|
|
|
// ============================================================================
|
|
|
|
|
|
|
|
|
|
// ACE tick function (called periodically, ~150ms interval)
|
|
|
|
|
// Observes metrics and decides promotion (1MB→2MB) or demotion (2MB→1MB)
|
|
|
|
|
void hak_tiny_superslab_ace_tick(int class_idx, uint64_t now_ns);
|
|
|
|
|
|
|
|
|
|
// Phase 8.4: ACE Observer (called from Learner thread - zero hot-path overhead)
|
|
|
|
|
void hak_tiny_superslab_ace_observe_all(void);
|
|
|
|
|
|
2025-11-07 23:05:33 +09:00
|
|
|
// ============================================================================
|
2025-11-05 12:31:14 +09:00
|
|
|
// Partial SuperSlab adopt/publish (per-class single-slot)
|
2025-11-07 23:05:33 +09:00
|
|
|
// ============================================================================
|
|
|
|
|
|
2025-11-05 12:31:14 +09:00
|
|
|
// Publish a SuperSlab with available freelist for other threads to adopt.
|
|
|
|
|
void ss_partial_publish(int class_idx, SuperSlab* ss);
|
2025-11-07 23:05:33 +09:00
|
|
|
|
2025-11-05 12:31:14 +09:00
|
|
|
// Adopt published SuperSlab for the class (returns NULL if none).
|
|
|
|
|
SuperSlab* ss_partial_adopt(int class_idx);
|
|
|
|
|
|
2025-11-07 23:05:33 +09:00
|
|
|
// ============================================================================
|
2025-11-05 12:31:14 +09:00
|
|
|
// SuperSlab adopt gate (publish/adopt wiring helper)
|
2025-11-07 23:05:33 +09:00
|
|
|
// ============================================================================
|
|
|
|
|
|
2025-11-05 12:31:14 +09:00
|
|
|
// Environment-aware switch that keeps free/alloc sides in sync. Default:
|
|
|
|
|
// - Disabled until cross-thread free is observed.
|
|
|
|
|
// - `HAKMEM_TINY_SS_ADOPT=1` forces ON, `=0` forces OFF.
|
|
|
|
|
int tiny_adopt_gate_should_publish(void);
|
|
|
|
|
int tiny_adopt_gate_should_adopt(void);
|
|
|
|
|
void tiny_adopt_gate_on_remote_seen(int class_idx);
|
|
|
|
|
|
2025-11-07 23:05:33 +09:00
|
|
|
// ============================================================================
|
|
|
|
|
// External variable declarations
|
|
|
|
|
// ============================================================================
|
2025-11-05 12:31:14 +09:00
|
|
|
|
2025-11-07 23:05:33 +09:00
|
|
|
extern _Atomic int g_ss_remote_seen; // set to 1 on first remote free observed
|
|
|
|
|
extern int g_remote_force_notify;
|
2025-11-05 12:31:14 +09:00
|
|
|
|
|
|
|
|
#endif // HAKMEM_TINY_SUPERSLAB_H
|