2025-12-06 01:34:04 +09:00
|
|
|
// tiny_class_stats_box.h - Lightweight per-thread class stats (OBSERVE layer)
|
|
|
|
|
//
|
|
|
|
|
// Purpose:
|
|
|
|
|
// - Provide per-class counters without atomics for cheap observation.
|
|
|
|
|
// - Hot paths call small inline helpers; aggregation/printing can be added later.
|
|
|
|
|
|
|
|
|
|
#ifndef TINY_CLASS_STATS_BOX_H
|
|
|
|
|
#define TINY_CLASS_STATS_BOX_H
|
|
|
|
|
|
|
|
|
|
#include <stdint.h>
|
2025-12-06 01:44:05 +09:00
|
|
|
#include <stdatomic.h>
|
|
|
|
|
#include <stdio.h>
|
2025-12-06 01:34:04 +09:00
|
|
|
#include "../hakmem_tiny_config.h"
|
|
|
|
|
|
|
|
|
|
typedef struct TinyClassStatsThread {
|
|
|
|
|
uint64_t uc_miss[TINY_NUM_CLASSES]; // unified_cache_refill() hits
|
|
|
|
|
uint64_t warm_hit[TINY_NUM_CLASSES]; // warm pool successes
|
|
|
|
|
uint64_t shared_lock[TINY_NUM_CLASSES]; // shared pool lock acquisitions (hook as needed)
|
|
|
|
|
} TinyClassStatsThread;
|
|
|
|
|
|
|
|
|
|
extern __thread TinyClassStatsThread g_tiny_class_stats;
|
|
|
|
|
|
2025-12-06 01:44:05 +09:00
|
|
|
// Global (cross-thread) aggregates for OBSERVE/LEARN
|
|
|
|
|
extern _Atomic uint64_t g_tiny_class_stats_uc_miss_global[TINY_NUM_CLASSES];
|
|
|
|
|
extern _Atomic uint64_t g_tiny_class_stats_warm_hit_global[TINY_NUM_CLASSES];
|
|
|
|
|
extern _Atomic uint64_t g_tiny_class_stats_shared_lock_global[TINY_NUM_CLASSES];
|
|
|
|
|
|
2025-12-06 01:34:04 +09:00
|
|
|
static inline void tiny_class_stats_on_uc_miss(int ci) {
|
|
|
|
|
if (ci >= 0 && ci < TINY_NUM_CLASSES) {
|
|
|
|
|
g_tiny_class_stats.uc_miss[ci]++;
|
2025-12-06 01:44:05 +09:00
|
|
|
atomic_fetch_add_explicit(&g_tiny_class_stats_uc_miss_global[ci],
|
|
|
|
|
1, memory_order_relaxed);
|
2025-12-06 01:34:04 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline void tiny_class_stats_on_warm_hit(int ci) {
|
|
|
|
|
if (ci >= 0 && ci < TINY_NUM_CLASSES) {
|
|
|
|
|
g_tiny_class_stats.warm_hit[ci]++;
|
2025-12-06 01:44:05 +09:00
|
|
|
atomic_fetch_add_explicit(&g_tiny_class_stats_warm_hit_global[ci],
|
|
|
|
|
1, memory_order_relaxed);
|
2025-12-06 01:34:04 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline void tiny_class_stats_on_shared_lock(int ci) {
|
|
|
|
|
if (ci >= 0 && ci < TINY_NUM_CLASSES) {
|
|
|
|
|
g_tiny_class_stats.shared_lock[ci]++;
|
2025-12-06 01:44:05 +09:00
|
|
|
atomic_fetch_add_explicit(&g_tiny_class_stats_shared_lock_global[ci],
|
|
|
|
|
1, memory_order_relaxed);
|
2025-12-06 01:34:04 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Optional: reset per-thread counters (cold path only).
|
|
|
|
|
void tiny_class_stats_reset_thread(void);
|
|
|
|
|
|
2025-12-06 01:44:05 +09:00
|
|
|
// Snapshot helpers (cold path): copy current counters into caller-provided struct.
|
|
|
|
|
void tiny_class_stats_snapshot_thread(TinyClassStatsThread* out);
|
|
|
|
|
void tiny_class_stats_snapshot_global(TinyClassStatsThread* out);
|
|
|
|
|
|
|
|
|
|
// Simple stderr dump helpers (cold path)
|
|
|
|
|
void tiny_class_stats_dump_thread(FILE* out, const char* tag);
|
|
|
|
|
void tiny_class_stats_dump_global(FILE* out, const char* tag);
|
|
|
|
|
|
2025-12-06 01:34:04 +09:00
|
|
|
#endif // TINY_CLASS_STATS_BOX_H
|