liquid_cache_storage/cache/
stats.rs

1use std::path::PathBuf;
2use std::sync::atomic::{AtomicU64, Ordering};
3
4/// Atomic runtime counters for cache API calls.
5#[derive(Debug, Default)]
6pub struct RuntimeStats {
7    /// Number of `get_arrow_array` calls issued via `CachedData`.
8    pub(crate) get_arrow_array_calls: AtomicU64,
9    /// Number of `get_with_selection` calls issued via `CachedData`.
10    pub(crate) get_with_selection_calls: AtomicU64,
11    /// Number of `get_with_predicate` calls issued via `CachedData`.
12    pub(crate) get_with_predicate_calls: AtomicU64,
13    /// Number of Hybrid-Liquid predicate evaluations finished without IO.
14    pub(crate) get_predicate_hybrid_success: AtomicU64,
15    /// Number of Hybrid-Liquid predicate paths that required IO.
16    pub(crate) get_predicate_hybrid_needs_io: AtomicU64,
17    /// Number of Hybrid-Liquid predicate paths that were unsupported and fell back.
18    pub(crate) get_predicate_hybrid_unsupported: AtomicU64,
19    /// Number of `try_read_liquid` calls issued via `CachedData`.
20    pub(crate) try_read_liquid_calls: AtomicU64,
21}
22
23/// Immutable snapshot of [`RuntimeStats`].
24#[derive(Debug, Clone, Copy)]
25pub struct RuntimeStatsSnapshot {
26    /// Total `get_arrow_array` calls.
27    pub get_arrow_array_calls: u64,
28    /// Total `get_with_selection` calls.
29    pub get_with_selection_calls: u64,
30    /// Total `get_with_predicate` calls.
31    pub get_with_predicate_calls: u64,
32    /// Total Hybrid-Liquid predicate successes (no IO).
33    pub get_predicate_hybrid_success: u64,
34    /// Total Hybrid-Liquid predicate paths requiring IO.
35    pub get_predicate_hybrid_needs_io: u64,
36    /// Total Hybrid-Liquid predicate paths that were unsupported.
37    pub get_predicate_hybrid_unsupported: u64,
38    /// Total `try_read_liquid` calls.
39    pub try_read_liquid_calls: u64,
40}
41
42impl RuntimeStats {
43    /// Return an immutable snapshot of the current runtime counters and reset the stats to 0.
44    pub fn consume_snapshot(&self) -> RuntimeStatsSnapshot {
45        let v = RuntimeStatsSnapshot {
46            get_arrow_array_calls: self.get_arrow_array_calls.load(Ordering::Relaxed),
47            get_with_selection_calls: self.get_with_selection_calls.load(Ordering::Relaxed),
48            get_with_predicate_calls: self.get_with_predicate_calls.load(Ordering::Relaxed),
49            get_predicate_hybrid_success: self.get_predicate_hybrid_success.load(Ordering::Relaxed),
50            get_predicate_hybrid_needs_io: self
51                .get_predicate_hybrid_needs_io
52                .load(Ordering::Relaxed),
53            get_predicate_hybrid_unsupported: self
54                .get_predicate_hybrid_unsupported
55                .load(Ordering::Relaxed),
56            try_read_liquid_calls: self.try_read_liquid_calls.load(Ordering::Relaxed),
57        };
58        self.reset();
59        v
60    }
61
62    /// Increment `get_arrow_array` counter.
63    #[inline]
64    pub fn incr_get_arrow_array(&self) {
65        self.get_arrow_array_calls.fetch_add(1, Ordering::Relaxed);
66    }
67
68    /// Increment `get_with_selection` counter.
69    #[inline]
70    pub fn incr_get_with_selection(&self) {
71        self.get_with_selection_calls
72            .fetch_add(1, Ordering::Relaxed);
73    }
74
75    /// Increment `get_with_predicate` counter.
76    #[inline]
77    pub fn incr_get_with_predicate(&self) {
78        self.get_with_predicate_calls
79            .fetch_add(1, Ordering::Relaxed);
80    }
81
82    /// Increment `try_read_liquid` counter.
83    #[inline]
84    pub fn incr_try_read_liquid(&self) {
85        self.try_read_liquid_calls.fetch_add(1, Ordering::Relaxed);
86    }
87
88    /// Increment Hybrid-Liquid predicate success counter.
89    #[inline]
90    pub fn incr_get_predicate_hybrid_success(&self) {
91        self.get_predicate_hybrid_success
92            .fetch_add(1, Ordering::Relaxed);
93    }
94
95    /// Increment Hybrid-Liquid predicate needs-IO counter.
96    #[inline]
97    pub fn incr_get_predicate_hybrid_needs_io(&self) {
98        self.get_predicate_hybrid_needs_io
99            .fetch_add(1, Ordering::Relaxed);
100    }
101
102    /// Increment Hybrid-Liquid predicate unsupported counter.
103    #[inline]
104    pub fn incr_get_predicate_hybrid_unsupported(&self) {
105        self.get_predicate_hybrid_unsupported
106            .fetch_add(1, Ordering::Relaxed);
107    }
108
109    /// Reset the runtime stats to 0.
110    pub fn reset(&self) {
111        self.get_arrow_array_calls.store(0, Ordering::Relaxed);
112        self.get_with_selection_calls.store(0, Ordering::Relaxed);
113        self.get_with_predicate_calls.store(0, Ordering::Relaxed);
114        self.get_predicate_hybrid_success
115            .store(0, Ordering::Relaxed);
116        self.get_predicate_hybrid_needs_io
117            .store(0, Ordering::Relaxed);
118        self.get_predicate_hybrid_unsupported
119            .store(0, Ordering::Relaxed);
120        self.try_read_liquid_calls.store(0, Ordering::Relaxed);
121    }
122}
123
124/// Snapshot of cache statistics.
125#[derive(Debug, Clone)]
126pub struct CacheStats {
127    /// Total number of entries in the cache.
128    pub total_entries: usize,
129    /// Number of in-memory Arrow entries.
130    pub memory_arrow_entries: usize,
131    /// Number of in-memory Liquid entries.
132    pub memory_liquid_entries: usize,
133    /// Number of in-memory Hybrid-Liquid entries.
134    pub memory_hybrid_liquid_entries: usize,
135    /// Number of on-disk Liquid entries.
136    pub disk_liquid_entries: usize,
137    /// Number of on-disk Arrow entries.
138    pub disk_arrow_entries: usize,
139    /// Total memory usage of the cache.
140    pub memory_usage_bytes: usize,
141    /// Total disk usage of the cache.
142    pub disk_usage_bytes: usize,
143    /// Maximum cache size.
144    pub max_cache_bytes: usize,
145    /// Cache root directory.
146    pub cache_root_dir: PathBuf,
147    /// Runtime counters snapshot.
148    pub runtime: RuntimeStatsSnapshot,
149}