2025-12-08 21:30:21 +09:00
|
|
|
// tiny_front_stats_box.h - Front class distribution counters (ENV gated)
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <stdatomic.h>
|
2025-12-13 18:46:11 +09:00
|
|
|
#include "../hakmem_build_flags.h"
|
2025-12-08 21:30:21 +09:00
|
|
|
#include "../hakmem_tiny_config.h"
|
|
|
|
|
|
|
|
|
|
extern _Atomic uint64_t g_tiny_front_alloc_class[TINY_NUM_CLASSES];
|
|
|
|
|
extern _Atomic uint64_t g_tiny_front_free_class[TINY_NUM_CLASSES];
|
|
|
|
|
|
|
|
|
|
static inline int tiny_front_class_stats_enabled(void) {
|
2025-12-13 18:46:11 +09:00
|
|
|
#if !HAKMEM_DEBUG_COUNTERS
|
|
|
|
|
return 0;
|
|
|
|
|
#else
|
2025-12-08 21:30:21 +09:00
|
|
|
static int g = -1;
|
|
|
|
|
if (__builtin_expect(g == -1, 0)) {
|
|
|
|
|
const char* e = getenv("HAKMEM_TINY_FRONT_CLASS_STATS");
|
|
|
|
|
g = (e && *e && *e != '0') ? 1 : 0;
|
|
|
|
|
}
|
|
|
|
|
return g;
|
2025-12-13 18:46:11 +09:00
|
|
|
#endif
|
2025-12-08 21:30:21 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline int tiny_front_class_stats_dump_enabled(void) {
|
2025-12-13 18:46:11 +09:00
|
|
|
#if !HAKMEM_DEBUG_COUNTERS
|
|
|
|
|
return 0;
|
|
|
|
|
#else
|
2025-12-08 21:30:21 +09:00
|
|
|
static int g = -1;
|
|
|
|
|
if (__builtin_expect(g == -1, 0)) {
|
|
|
|
|
const char* e = getenv("HAKMEM_TINY_FRONT_CLASS_STATS_DUMP");
|
|
|
|
|
const char* e2 = getenv("HAKMEM_TINY_FRONT_CLASS_STATS");
|
|
|
|
|
g = ((e && *e && *e != '0') || (e2 && *e2 && *e2 != '0')) ? 1 : 0;
|
|
|
|
|
}
|
|
|
|
|
return g;
|
2025-12-13 18:46:11 +09:00
|
|
|
#endif
|
2025-12-08 21:30:21 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline void tiny_front_alloc_stat_inc(int class_idx) {
|
2025-12-13 18:46:11 +09:00
|
|
|
#if HAKMEM_DEBUG_COUNTERS
|
2025-12-08 21:30:21 +09:00
|
|
|
if (__builtin_expect(tiny_front_class_stats_enabled(), 0)) {
|
|
|
|
|
atomic_fetch_add_explicit(&g_tiny_front_alloc_class[class_idx], 1, memory_order_relaxed);
|
|
|
|
|
}
|
2025-12-13 18:46:11 +09:00
|
|
|
#else
|
|
|
|
|
(void)class_idx;
|
|
|
|
|
#endif
|
2025-12-08 21:30:21 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline void tiny_front_free_stat_inc(int class_idx) {
|
2025-12-13 18:46:11 +09:00
|
|
|
#if HAKMEM_DEBUG_COUNTERS
|
2025-12-08 21:30:21 +09:00
|
|
|
if (__builtin_expect(tiny_front_class_stats_enabled(), 0)) {
|
|
|
|
|
atomic_fetch_add_explicit(&g_tiny_front_free_class[class_idx], 1, memory_order_relaxed);
|
|
|
|
|
}
|
2025-12-13 18:46:11 +09:00
|
|
|
#else
|
|
|
|
|
(void)class_idx;
|
|
|
|
|
#endif
|
2025-12-08 21:30:21 +09:00
|
|
|
}
|