Files
hakmem/core/hakmem_ace_metrics.h

84 lines
2.8 KiB
C
Raw Normal View History

/* hakmem_ace_metrics.h - ACE Learning Layer Metrics Collection
*
* :
* - Fast metrics (1Hz): throughput, LLC miss, mutex wait, backlog
* - Slow metrics (60s): fragmentation ratio, RSS
* - Inline helpers for hot-path tracking
*/
#ifndef HAKMEM_ACE_METRICS_H
#define HAKMEM_ACE_METRICS_H
#include <stdint.h>
#include <stdbool.h>
#include <stdatomic.h>
#include <time.h>
/* メトリクス構造体 */
struct hkm_ace_metrics {
/* Fast metrics (updated every 0.5-1s) */
uint64_t throughput_ops; /* Operations per second */
double llc_miss_rate; /* LLC miss rate (0.0-1.0) */
uint64_t mutex_wait_ns; /* Cumulative mutex contention time */
uint32_t remote_free_backlog[8]; /* Per-class remote free backlog */
/* Slow metrics (updated every 60s) */
double fragmentation_ratio; /* allocated / reserved bytes */
uint64_t rss_mb; /* Resident set size in MB */
/* Timestamp */
uint64_t timestamp_ms; /* Collection timestamp */
};
/* グローバルカウンタhot-path用 */
extern _Atomic uint64_t g_ace_alloc_count;
extern _Atomic uint64_t g_ace_free_count;
extern _Atomic uint64_t g_ace_mutex_wait_ns;
/* 初期化・破棄 */
void hkm_ace_metrics_init(void);
void hkm_ace_metrics_destroy(void);
/* メトリクス収集 */
void hkm_ace_metrics_collect(struct hkm_ace_metrics *out);
void hkm_ace_metrics_collect_fast(struct hkm_ace_metrics *out);
void hkm_ace_metrics_collect_slow(struct hkm_ace_metrics *out);
/* Hot-path inline helpers */
/* アロケーションカウント(高速インクリメント) */
static inline void hkm_ace_track_alloc(void) {
atomic_fetch_add_explicit(&g_ace_alloc_count, 1, memory_order_relaxed);
}
/* フリーカウント(高速インクリメント) */
static inline void hkm_ace_track_free(void) {
atomic_fetch_add_explicit(&g_ace_free_count, 1, memory_order_relaxed);
}
/* ミューテックス待機時間トラッキング
* 使:
* uint64_t t0 = hkm_ace_mutex_timer_start();
* pthread_mutex_lock(&lock);
* hkm_ace_mutex_timer_end(t0);
*/
static inline uint64_t hkm_ace_mutex_timer_start(void) {
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (uint64_t)ts.tv_sec * 1000000000ULL + (uint64_t)ts.tv_nsec;
}
static inline void hkm_ace_mutex_timer_end(uint64_t start_ns) {
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
uint64_t end_ns = (uint64_t)ts.tv_sec * 1000000000ULL + (uint64_t)ts.tv_nsec;
uint64_t delta = (end_ns > start_ns) ? (end_ns - start_ns) : 0;
atomic_fetch_add_explicit(&g_ace_mutex_wait_ns, delta, memory_order_relaxed);
}
/* LLC miss monitoring (rdpmc wrapper) */
bool hkm_ace_llc_available(void);
void hkm_ace_llc_read(uint64_t *misses, uint64_t *references);
#endif /* HAKMEM_ACE_METRICS_H */