cache2 0.6.1

Performance-first bounded RAM and NVMe hybrid cache
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
// Copyright 2026 ScopeDB, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Managed-memory accounting and aligned record buffers.
//!
//! Allocations for L2 read paths (foreground point reads and fixed reclaim
//! buffers) use alignment-rounded buffers charged against the hard limit.

use std::alloc::Layout;
use std::alloc::alloc;
use std::alloc::dealloc;
use std::error;
use std::fmt;
use std::io;
use std::ptr::NonNull;
use std::slice;
use std::sync::Arc;
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering;

pub const BUFFER_ALIGNMENT: usize = 4096;
/// Every cache-owned thread uses an explicit stack reservation so configured
/// topology cannot inherit an environment-dependent `RUST_MIN_STACK` value.
pub const CACHE_THREAD_STACK_BYTES: usize = 512 * 1024;

pub struct ManagedMemoryLimits {
    pub memory_limit_bytes: usize,
    pub reserved_memory_bytes: usize,
}

#[derive(Debug)]
pub enum ManagedMemoryError {
    Invalid(&'static str),
    Allocation,
}

impl fmt::Display for ManagedMemoryError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Invalid(message) => formatter.write_str(message),
            Self::Allocation => formatter.write_str("managed memory cannot be allocated"),
        }
    }
}

impl error::Error for ManagedMemoryError {}

impl From<ManagedMemoryError> for io::Error {
    fn from(error: ManagedMemoryError) -> Self {
        let kind = match error {
            ManagedMemoryError::Invalid(_) => io::ErrorKind::InvalidInput,
            ManagedMemoryError::Allocation => io::ErrorKind::OutOfMemory,
        };
        Self::new(kind, error)
    }
}

pub struct ManagedMemory {
    memory: Arc<MemoryTracker>,
}

/// A charge against the cache-wide memory limit. Keep this guard until the
/// associated allocation has been released.
pub struct MemoryReservation {
    memory: Arc<MemoryTracker>,
    bytes: usize,
}

impl Drop for MemoryReservation {
    fn drop(&mut self) {
        self.memory.release(self.bytes);
    }
}

impl ManagedMemory {
    pub fn try_new(limits: ManagedMemoryLimits) -> Result<Self, ManagedMemoryError> {
        if limits.reserved_memory_bytes > limits.memory_limit_bytes {
            return Err(ManagedMemoryError::Invalid(
                "memory limit cannot hold the cache's reserved memory",
            ));
        }

        let memory = Arc::new(MemoryTracker::new(
            limits.memory_limit_bytes,
            limits.reserved_memory_bytes,
        ));
        Ok(Self { memory })
    }

    pub fn reserve(&self, bytes: usize) -> Result<MemoryReservation, ManagedMemoryError> {
        if !self.memory.try_reserve(bytes) {
            return Err(ManagedMemoryError::Allocation);
        }
        Ok(MemoryReservation {
            memory: Arc::clone(&self.memory),
            bytes,
        })
    }

    /// Allocates an alignment-rounded L2 read buffer against the cache-wide
    /// hard memory limit. Foreground callers map allocation failure to either
    /// a fail-open miss or bounded-wait overload; runtime startup allocates
    /// fixed reclaim buffers from the same helper.
    pub fn try_read_buffer(&self, length: usize) -> Option<BufferLease> {
        let capacity = length.checked_next_multiple_of(BUFFER_ALIGNMENT)?;
        if capacity == 0 || capacity > isize::MAX as usize {
            return None;
        }
        let reservation = self.reserve(capacity).ok()?;
        BufferLease::try_new(capacity, Some(reservation))
    }

    pub fn snapshot(&self) -> ManagedMemorySnapshot {
        let current_bytes = self.memory.current.load(Ordering::Relaxed);
        ManagedMemorySnapshot {
            limit_bytes: self.memory.limit,
            current_bytes,
            peak_bytes: self.memory.peak.load(Ordering::Relaxed).max(current_bytes),
        }
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct ManagedMemorySnapshot {
    pub limit_bytes: usize,
    pub current_bytes: usize,
    pub peak_bytes: usize,
}

pub struct BufferLease {
    ptr: NonNull<u8>,
    capacity: usize,
    initialized: usize,
    // Drop frees the allocation before this field returns its budget. Fixed
    // staging buffers use their owner's aggregate reservation instead.
    _reservation: Option<MemoryReservation>,
}

// SAFETY: ownership of the allocation moves with the lease. An I/O request
// retains exclusive ownership while a driver may access its raw pointer.
unsafe impl Send for BufferLease {}

impl BufferLease {
    pub fn try_fixed(length: usize) -> Result<Self, ManagedMemoryError> {
        if length == 0 || !length.is_multiple_of(BUFFER_ALIGNMENT) || length > isize::MAX as usize {
            return Err(ManagedMemoryError::Invalid(
                "fixed buffer size must be a non-zero 4096-byte multiple",
            ));
        }
        let mut buffer = Self::try_new(length, None).ok_or(ManagedMemoryError::Allocation)?;
        // SAFETY: the allocation is exclusively owned and valid for `length`
        // bytes. Initialize it before exposing any Rust references.
        unsafe { buffer.ptr.as_ptr().write_bytes(0, length) };
        buffer.initialized = length;
        Ok(buffer)
    }

    fn try_new(capacity: usize, reservation: Option<MemoryReservation>) -> Option<Self> {
        if capacity == 0 {
            return None;
        }
        let layout = Layout::from_size_align(capacity, BUFFER_ALIGNMENT).ok()?;
        // SAFETY: the layout has non-zero size and valid power-of-two alignment.
        let ptr = NonNull::new(unsafe { alloc(layout) })?;
        Some(Self {
            ptr,
            capacity,
            initialized: 0,
            _reservation: reservation,
        })
    }

    pub fn prepared(&self, length: usize) -> Option<&[u8]> {
        if length > self.initialized {
            return None;
        }
        // SAFETY: the allocation holds `initialized` initialized bytes and the
        // returned shared slice cannot mutate the exclusively leased buffer.
        Some(unsafe { slice::from_raw_parts(self.ptr.as_ptr(), length) })
    }

    pub fn prepared_mut(&mut self, length: usize) -> Option<&mut [u8]> {
        if length > self.initialized {
            return None;
        }
        // SAFETY: the allocation holds `initialized` initialized bytes, this
        // mutable borrow is exclusive, and `length <= initialized`.
        Some(unsafe { slice::from_raw_parts_mut(self.ptr.as_ptr(), length) })
    }

    pub fn has_capacity(&self, length: usize) -> bool {
        length <= self.capacity
    }

    pub const fn as_mut_ptr(&self) -> *mut u8 {
        self.ptr.as_ptr()
    }

    pub fn mark_initialized(&mut self, length: usize) -> bool {
        if length > self.capacity {
            return false;
        }
        self.initialized = self.initialized.max(length);
        true
    }
}

impl Drop for BufferLease {
    fn drop(&mut self) {
        let layout = Layout::from_size_align(self.capacity, BUFFER_ALIGNMENT)
            .expect("stored aligned-buffer layout is valid");
        // SAFETY: this lease owns the pointer allocated with this exact layout.
        // Its reservation is released by field drop after deallocation.
        unsafe { dealloc(self.ptr.as_ptr(), layout) };
    }
}

struct MemoryTracker {
    limit: usize,
    current: AtomicUsize,
    peak: AtomicUsize,
}

impl MemoryTracker {
    fn new(limit: usize, reserved: usize) -> Self {
        Self {
            limit,
            current: AtomicUsize::new(reserved),
            peak: AtomicUsize::new(reserved),
        }
    }

    fn try_reserve(&self, bytes: usize) -> bool {
        let mut current = self.current.load(Ordering::Relaxed);
        for _ in 0..MAX_ATOMIC_UPDATE_ATTEMPTS {
            let Some(next) = current.checked_add(bytes) else {
                return false;
            };
            if next > self.limit {
                return false;
            }
            match self
                .current
                .compare_exchange(current, next, Ordering::Relaxed, Ordering::Relaxed)
            {
                Ok(_) => {
                    update_peak(&self.peak, next);
                    return true;
                }
                Err(observed) => current = observed,
            }
        }
        false
    }

    fn release(&self, bytes: usize) {
        if bytes != 0 {
            self.current.fetch_sub(bytes, Ordering::Relaxed);
        }
    }
}

fn update_peak(peak: &AtomicUsize, candidate: usize) {
    // The peak only grows; a stale lower observation can cause an extra RMW,
    // but cannot suppress a new maximum.
    if candidate > peak.load(Ordering::Relaxed) {
        peak.fetch_max(candidate, Ordering::Relaxed);
    }
}

const MAX_ATOMIC_UPDATE_ATTEMPTS: usize = 8;

#[cfg(test)]
mod tests {
    use super::*;

    fn limits() -> ManagedMemoryLimits {
        ManagedMemoryLimits {
            memory_limit_bytes: 128 * 1024,
            reserved_memory_bytes: 16 * 1024,
        }
    }

    #[test]
    fn transient_read_buffers_charge_exact_aligned_bytes_and_release_them() {
        let managed_memory = ManagedMemory::try_new(limits()).unwrap();
        let first = managed_memory.try_read_buffer(5000).unwrap();
        let second = managed_memory.try_read_buffer(9000).unwrap();
        assert_eq!(first.as_mut_ptr() as usize % BUFFER_ALIGNMENT, 0);
        assert_eq!(second.as_mut_ptr() as usize % BUFFER_ALIGNMENT, 0);
        assert!(first.prepared(5000).is_none());
        assert!(second.prepared(9000).is_none());
        let snapshot = managed_memory.snapshot();
        assert_eq!(
            snapshot.current_bytes,
            16 * 1024 + 2 * BUFFER_ALIGNMENT + 3 * BUFFER_ALIGNMENT
        );
        assert!(snapshot.peak_bytes <= snapshot.limit_bytes);
        drop(first);
        assert_eq!(
            managed_memory.snapshot().current_bytes,
            16 * 1024 + 3 * BUFFER_ALIGNMENT
        );
        drop(second);
        assert_eq!(managed_memory.snapshot().current_bytes, 16 * 1024);
    }

    #[test]
    fn read_buffers_expose_only_the_initialized_prefix() {
        let managed_memory = ManagedMemory::try_new(limits()).unwrap();
        let mut buffer = managed_memory.try_read_buffer(5000).unwrap();
        let bytes = b"completed read prefix";
        let target = buffer.as_mut_ptr();
        // SAFETY: simulate a completed read into the exclusively owned target;
        // the source and destination do not overlap and the bytes fit.
        unsafe { target.copy_from_nonoverlapping(bytes.as_ptr(), bytes.len()) };

        assert!(buffer.prepared(bytes.len()).is_none());
        assert!(buffer.prepared_mut(bytes.len()).is_none());
        assert!(buffer.mark_initialized(bytes.len()));
        assert_eq!(buffer.prepared(bytes.len()).unwrap(), bytes);
        assert!(buffer.prepared(bytes.len() + 1).is_none());
        assert!(buffer.prepared_mut(bytes.len() + 1).is_none());

        // Reusing the buffer for a shorter read preserves initialized bytes.
        assert!(buffer.mark_initialized(1));
        assert_eq!(buffer.prepared(bytes.len()).unwrap(), bytes);
        assert!(buffer.has_capacity(2 * BUFFER_ALIGNMENT));
        assert!(!buffer.has_capacity(2 * BUFFER_ALIGNMENT + 1));
        assert!(!buffer.mark_initialized(2 * BUFFER_ALIGNMENT + 1));
        assert!(buffer.prepared(bytes.len() + 1).is_none());
    }

    #[test]
    fn fixed_buffers_are_zeroed_and_reject_out_of_bounds_access() {
        let mut buffer = BufferLease::try_fixed(BUFFER_ALIGNMENT).unwrap();
        assert!(
            buffer
                .prepared(BUFFER_ALIGNMENT)
                .unwrap()
                .iter()
                .all(|byte| *byte == 0)
        );
        buffer.prepared_mut(BUFFER_ALIGNMENT).unwrap().fill(0xa5);
        assert!(!buffer.has_capacity(BUFFER_ALIGNMENT + 1));
        assert!(buffer.prepared(BUFFER_ALIGNMENT + 1).is_none());
        assert!(buffer.prepared_mut(BUFFER_ALIGNMENT + 1).is_none());
        assert!(
            buffer
                .prepared(BUFFER_ALIGNMENT)
                .unwrap()
                .iter()
                .all(|byte| *byte == 0xa5)
        );
    }

    #[test]
    fn invalid_read_buffer_sizes_do_not_consume_memory() {
        let managed_memory = ManagedMemory::try_new(limits()).unwrap();
        let before = managed_memory.snapshot();
        for length in [0, isize::MAX as usize, usize::MAX] {
            assert!(managed_memory.try_read_buffer(length).is_none());
            assert_eq!(managed_memory.snapshot(), before);
        }
    }

    #[test]
    fn transient_read_buffers_stay_within_the_memory_limit() {
        let mut configured = limits();
        configured.memory_limit_bytes = configured.reserved_memory_bytes + 3 * BUFFER_ALIGNMENT;

        let managed_memory = ManagedMemory::try_new(configured).unwrap();
        let first = managed_memory.try_read_buffer(BUFFER_ALIGNMENT).unwrap();
        let second = managed_memory
            .try_read_buffer(2 * BUFFER_ALIGNMENT)
            .unwrap();
        assert!(managed_memory.try_read_buffer(1).is_none());
        let memory = managed_memory.snapshot();
        assert_eq!(memory.current_bytes, memory.limit_bytes);
        assert!(memory.peak_bytes <= memory.limit_bytes);
        drop((first, second));
        assert!(
            managed_memory
                .try_read_buffer(3 * BUFFER_ALIGNMENT)
                .is_some()
        );
    }

    #[test]
    fn reserved_memory_over_limit_is_rejected_before_allocating_buffers() {
        let mut too_small = limits();
        too_small.memory_limit_bytes = too_small.reserved_memory_bytes - 1;
        assert!(matches!(
            ManagedMemory::try_new(too_small),
            Err(ManagedMemoryError::Invalid(_))
        ));
    }
}