Phase ALLOC-GATE-OPT-1: tiny_alloc_gate_fast 統計計測

- AllocGateStats 構造体追加(size2class/route/env/class分布)
- malloc_tiny_fast にカウンタ埋め込み
- ENV: HAKMEM_ALLOC_GATE_STATS (default 0)
- 挙動変更なし(計測のみ)

計測結果:
- Mixed: total=542k, size2class=0, route_calls=0, env_checks=275k, C4-C7=95.2%
  - size_to_class/route_for_class は完全削減済み(LUT 効果)
  - C4-C7 が 95% → ULTRA fast path が有効
  - env_checks ≈ c7_calls → C7 ULTRA の ENV gate が毎回呼ばれる
- C6-heavy: total=11 → malloc_tiny_fast はほぼ通らない(mid/pool 主体)

結論:
- alloc gate は既に十分最適化済み(LUT + ULTRA で削減済み)
- さらなる最適化余地は小さい(env_checks は軽量化済み、数%以下の効果)
- 次フェーズでは free dispatcher (29%) や C7 ULTRA refill (7%) など、他のボトルネックを狙う

詳細: docs/analysis/ALLOC_GATE_ANALYSIS.md

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

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
This commit is contained in:
Moe Charm (CI)
2025-12-11 21:32:40 +09:00
parent 118c0e4857
commit 0f15adae4e
8 changed files with 270 additions and 3 deletions

View File

@ -0,0 +1,27 @@
#include "alloc_gate_stats_box.h"
#include <stdio.h>
AllocGateStats g_alloc_gate_stats = {0};
__attribute__((destructor))
static void alloc_gate_stats_dump(void) {
if (!alloc_gate_stats_enabled()) {
return;
}
fprintf(stderr, "[ALLOC_GATE_STATS] total=%lu size2class=%lu route_calls=%lu env_checks=%lu c0=%lu c1=%lu c2=%lu c3=%lu c4=%lu c5=%lu c6=%lu c7=%lu\n",
g_alloc_gate_stats.total_calls,
g_alloc_gate_stats.size_to_class_calls,
g_alloc_gate_stats.route_for_class_calls,
g_alloc_gate_stats.env_checks,
g_alloc_gate_stats.class_calls[0],
g_alloc_gate_stats.class_calls[1],
g_alloc_gate_stats.class_calls[2],
g_alloc_gate_stats.class_calls[3],
g_alloc_gate_stats.class_calls[4],
g_alloc_gate_stats.class_calls[5],
g_alloc_gate_stats.class_calls[6],
g_alloc_gate_stats.class_calls[7]);
fflush(stderr);
}

View File

@ -0,0 +1,43 @@
#ifndef HAKMEM_ALLOC_GATE_STATS_BOX_H
#define HAKMEM_ALLOC_GATE_STATS_BOX_H
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
typedef struct AllocGateStats {
uint64_t total_calls; // malloc_tiny_fast 入口
uint64_t size_to_class_calls; // size→class 変換
uint64_t route_for_class_calls; // class→route 判定
uint64_t env_checks; // alloc-side ENV 判定数
// クラス別分布
uint64_t class_calls[8]; // C0〜C7 呼び出し数
} AllocGateStats;
// ENV gate
static inline bool alloc_gate_stats_enabled(void) {
static int g_enabled = -1;
if (__builtin_expect(g_enabled == -1, 0)) {
const char* e = getenv("HAKMEM_ALLOC_GATE_STATS");
g_enabled = (e && *e && *e != '0') ? 1 : 0;
}
return g_enabled;
}
// Global stats instance
extern AllocGateStats g_alloc_gate_stats;
// Increment macros (with unlikely guard)
#define ALLOC_GATE_STAT_INC(field) \
do { if (__builtin_expect(alloc_gate_stats_enabled(), 0)) { \
g_alloc_gate_stats.field++; \
} } while(0)
#define ALLOC_GATE_STAT_INC_CLASS(class_idx) \
do { if (__builtin_expect(alloc_gate_stats_enabled(), 0)) { \
if ((class_idx) >= 0 && (class_idx) < 8) g_alloc_gate_stats.class_calls[class_idx]++; \
} } while(0)
#endif // HAKMEM_ALLOC_GATE_STATS_BOX_H