Files
hakmem/core/box/alloc_gate_stats_box.h

44 lines
1.3 KiB
C
Raw Normal View History

#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