Skip to main content

ax_cache/
metrics.rs

1use core::sync::atomic::{AtomicU64, Ordering};
2
3#[repr(align(64))]
4#[derive(Default, Debug)]
5pub(crate) struct PaddedCounter(pub(crate) AtomicU64);
6
7#[derive(Default, Debug)]
8pub(crate) struct Metrics {
9    pub(crate) hits: PaddedCounter,
10    pub(crate) misses: PaddedCounter,
11    pub(crate) insertions: AtomicU64,
12    pub(crate) evictions: AtomicU64,
13    pub(crate) rejections: AtomicU64,
14}
15
16impl Metrics {
17    #[inline(always)]
18    pub(crate) fn hit(&self) {
19        self.hits.0.fetch_add(1, Ordering::Relaxed);
20    }
21
22    #[inline(always)]
23    pub(crate) fn miss(&self) {
24        self.misses.0.fetch_add(1, Ordering::Relaxed);
25    }
26
27    #[inline(always)]
28    pub(crate) fn insertion(&self) {
29        self.insertions.fetch_add(1, Ordering::Relaxed);
30    }
31
32    #[inline(always)]
33    pub(crate) fn eviction(&self) {
34        self.evictions.fetch_add(1, Ordering::Relaxed);
35    }
36
37    #[inline(always)]
38    pub(crate) fn rejection(&self) {
39        self.rejections.fetch_add(1, Ordering::Relaxed);
40    }
41
42    pub(crate) fn snapshot(&self) -> MetricsSnapshot {
43        MetricsSnapshot {
44            hits: self.hits.0.load(Ordering::Relaxed),
45            misses: self.misses.0.load(Ordering::Relaxed),
46            insertions: self.insertions.load(Ordering::Relaxed),
47            evictions: self.evictions.load(Ordering::Relaxed),
48            rejections: self.rejections.load(Ordering::Relaxed),
49        }
50    }
51}
52
53#[derive(Debug, Clone, Copy, Default)]
54pub struct MetricsSnapshot {
55    pub hits: u64,
56    pub misses: u64,
57    pub insertions: u64,
58    pub evictions: u64,
59    pub rejections: u64,
60}
61
62impl MetricsSnapshot {
63    #[inline]
64    pub(crate) fn merge(&mut self, other: &MetricsSnapshot) {
65        self.hits += other.hits;
66        self.misses += other.misses;
67        self.insertions += other.insertions;
68        self.evictions += other.evictions;
69        self.rejections += other.rejections;
70    }
71}