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
// Copyright Materialize, Inc. and contributors. All rights reserved.
//
// 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 in the LICENSE file at the
// root of this repository, or online 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.

//! The [bytes] crate but backed by [lgalloc].

use std::fmt::Debug;
use std::sync::Arc;
use std::time::Instant;

use bytes::Buf;
use lgalloc::AllocError;
use prometheus::{Counter, CounterVec, Histogram, IntCounter, IntCounterVec};
use tracing::debug;

use crate::cast::{CastFrom, CastLossy};
use crate::metric;
use crate::metrics::MetricsRegistry;
use crate::region::Region;

/// [bytes::Bytes] but backed by [lgalloc].
#[derive(Clone, Debug)]
pub struct LgBytes {
    offset: usize,
    region: Arc<MetricsRegion<u8>>,
}

/// A [Region] wrapper that increments metrics when it is dropped.
///
/// The `T: Copy` bound ensures that the `Region` doesn't leak resources when
/// dropped.
pub struct MetricsRegion<T: Copy> {
    buf: Region<T>,
    free_count: IntCounter,
    free_capacity_bytes: IntCounter,
}

impl<T: Copy + Debug> Debug for MetricsRegion<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        std::fmt::Debug::fmt(self.buf.as_vec(), f)
    }
}

impl<T: Copy> MetricsRegion<T> {
    fn capacity_bytes(&self) -> usize {
        self.buf.capacity() * std::mem::size_of::<T>()
    }
}

impl<T: Copy + PartialEq> PartialEq for MetricsRegion<T> {
    fn eq(&self, other: &Self) -> bool {
        self.buf.as_vec() == other.buf.as_vec()
    }
}

impl<T: Copy + Eq> Eq for MetricsRegion<T> {}

impl<T: Copy> Drop for MetricsRegion<T> {
    fn drop(&mut self) {
        self.free_count.inc();
        self.free_capacity_bytes
            .inc_by(u64::cast_from(self.capacity_bytes()));
    }
}

impl<T: Copy> AsRef<[T]> for MetricsRegion<T> {
    fn as_ref(&self) -> &[T] {
        &self.buf[..]
    }
}

impl From<Arc<MetricsRegion<u8>>> for LgBytes {
    fn from(region: Arc<MetricsRegion<u8>>) -> Self {
        LgBytes { offset: 0, region }
    }
}

impl AsRef<[u8]> for LgBytes {
    fn as_ref(&self) -> &[u8] {
        // This implementation of [bytes::Buf] chooses to panic instead of
        // allowing the offset to advance past remaining, which means this
        // invariant should always hold and we shouldn't need the std::cmp::min.
        // Be defensive anyway.
        debug_assert!(self.offset <= self.region.buf.len());
        let offset = std::cmp::min(self.offset, self.region.buf.len());
        &self.region.buf[offset..]
    }
}

impl std::ops::Deref for LgBytes {
    type Target = [u8];
    fn deref(&self) -> &Self::Target {
        self.as_ref()
    }
}

impl Buf for LgBytes {
    /// Returns the number of bytes between the current position and the end of
    /// the buffer.
    ///
    /// This value is greater than or equal to the length of the slice returned
    /// by `chunk()`.
    ///
    /// # Implementer notes
    ///
    /// Implementations of `remaining` should ensure that the return value does
    /// not change unless a call is made to `advance` or any other function that
    /// is documented to change the `Buf`'s current position.
    fn remaining(&self) -> usize {
        self.as_ref().len()
    }

    /// Returns a slice starting at the current position and of length between 0
    /// and `Buf::remaining()`. Note that this *can* return shorter slice (this
    /// allows non-continuous internal representation).
    ///
    /// This is a lower level function. Most operations are done with other
    /// functions.
    ///
    /// # Implementer notes
    ///
    /// This function should never panic. Once the end of the buffer is reached,
    /// i.e., `Buf::remaining` returns 0, calls to `chunk()` should return an
    /// empty slice.
    fn chunk(&self) -> &[u8] {
        self.as_ref()
    }

    /// Advance the internal cursor of the Buf
    ///
    /// The next call to `chunk()` will return a slice starting `cnt` bytes
    /// further into the underlying buffer.
    ///
    /// # Panics
    ///
    /// This function panics if `cnt > self.remaining()`.
    ///
    /// # Implementer notes
    ///
    /// It is recommended for implementations of `advance` to panic if `cnt >
    /// self.remaining()`. If the implementation does not panic, the call must
    /// behave as if `cnt == self.remaining()`.
    ///
    /// A call with `cnt == 0` should never panic and be a no-op.
    fn advance(&mut self, cnt: usize) {
        if cnt > self.remaining() {
            panic!(
                "cannot advance by {} only {} remaining",
                cnt,
                self.remaining()
            )
        };
        self.offset += cnt;
    }
}

/// Metrics for [LgBytes].
#[derive(Debug, Clone)]
pub struct LgBytesMetrics {
    /// Metrics for the "persist_s3" usage of [LgBytes].
    pub persist_s3: LgBytesOpMetrics,
    /// Metrics for the "persist_arrow" usage of [LgBytes].
    pub persist_arrow: LgBytesOpMetrics,
}

/// Metrics for an individual usage of [LgBytes].
#[derive(Clone)]
pub struct LgBytesOpMetrics {
    heap: LgBytesRegionMetrics,
    mmap: LgBytesRegionMetrics,
    alloc_seconds: Counter,
    mmap_disabled_count: IntCounter,
    mmap_error_count: IntCounter,
    // NB: Unlike the _bytes per-Region metrics, which are capacity, this is
    // intentionally the requested len.
    len_sizes: Histogram,
}

impl std::fmt::Debug for LgBytesOpMetrics {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("LgBytesOperationMetrics")
            .finish_non_exhaustive()
    }
}

#[derive(Clone)]
struct LgBytesRegionMetrics {
    alloc_count: IntCounter,
    alloc_capacity_bytes: IntCounter,
    free_count: IntCounter,
    free_capacity_bytes: IntCounter,
}

impl LgBytesMetrics {
    /// Returns a new [LgBytesMetrics] connected to the given metrics registry.
    pub fn new(registry: &MetricsRegistry) -> Self {
        let alloc_count: IntCounterVec = registry.register(metric!(
            name: "mz_lgbytes_alloc_count",
            help: "count of LgBytes allocations",
            var_labels: ["op", "region"],
        ));
        let alloc_capacity_bytes: IntCounterVec = registry.register(metric!(
            name: "mz_lgbytes_alloc_capacity_bytes",
            help: "total capacity bytes of LgBytes allocations",
            var_labels: ["op", "region"],
        ));
        let free_count: IntCounterVec = registry.register(metric!(
            name: "mz_lgbytes_free_count",
            help: "count of LgBytes frees",
            var_labels: ["op", "region"],
        ));
        let free_capacity_bytes: IntCounterVec = registry.register(metric!(
            name: "mz_lgbytes_free_capacity_bytes",
            help: "total capacity bytes of LgBytes frees",
            var_labels: ["op", "region"],
        ));
        let alloc_seconds: CounterVec = registry.register(metric!(
            name: "mz_lgbytes_alloc_seconds",
            help: "seconds spent getting LgBytes allocations and copying in data",
            var_labels: ["op"],
        ));
        let mmap_disabled_count: IntCounter = registry.register(metric!(
            name: "mz_bytes_mmap_disabled_count",
            help: "count alloc attempts with lgalloc disabled",
        ));
        let mmap_error_count: IntCounter = registry.register(metric!(
            name: "mz_bytes_mmap_error_count",
            help: "count of errors when attempting file-based mapped alloc",
        ));
        let len_sizes: Histogram = registry.register(metric!(
            name: "mz_bytes_alloc_len_sizes",
            help: "histogram of LgBytes alloc len sizes",
            buckets: crate::stats::HISTOGRAM_BYTE_BUCKETS.to_vec(),
        ));
        let op = |name: &str| LgBytesOpMetrics {
            heap: LgBytesRegionMetrics {
                alloc_count: alloc_count.with_label_values(&[name, "heap"]),
                alloc_capacity_bytes: alloc_capacity_bytes.with_label_values(&[name, "heap"]),
                free_count: free_count.with_label_values(&[name, "heap"]),
                free_capacity_bytes: free_capacity_bytes.with_label_values(&[name, "heap"]),
            },
            mmap: LgBytesRegionMetrics {
                alloc_count: alloc_count.with_label_values(&[name, "mmap"]),
                alloc_capacity_bytes: alloc_capacity_bytes.with_label_values(&[name, "mmap"]),
                free_count: free_count.with_label_values(&[name, "mmap"]),
                free_capacity_bytes: free_capacity_bytes.with_label_values(&[name, "mmap"]),
            },
            alloc_seconds: alloc_seconds.with_label_values(&[name]),
            mmap_disabled_count: mmap_disabled_count.clone(),
            mmap_error_count: mmap_error_count.clone(),
            len_sizes: len_sizes.clone(),
        };
        LgBytesMetrics {
            persist_s3: op("persist_s3"),
            persist_arrow: op("persist_arrow"),
        }
    }
}

impl LgBytesOpMetrics {
    /// Attempts to copy the given buf into an lgalloc managed file-based mapped
    /// region, falling back to a heap allocation.
    pub fn try_mmap<T: AsRef<[u8]>>(&self, buf: T) -> LgBytes {
        let region = self.try_mmap_region(buf);
        LgBytes::from(Arc::new(region))
    }

    /// Attempts to copy the given buf into an lgalloc managed file-based mapped
    /// region, falling back to a heap allocation.
    pub fn try_mmap_region<T: Copy>(&self, buf: impl AsRef<[T]>) -> MetricsRegion<T> {
        let start = Instant::now();
        let buf = buf.as_ref();
        // Round the capacity up to the minimum lgalloc mmap size.
        let capacity = std::cmp::max(buf.len(), 1 << lgalloc::VALID_SIZE_CLASS.start);
        let buf = match Region::new_mmap(capacity) {
            Ok(mut region) => {
                region.extend_from_slice(buf);
                region
            }
            Err(err) => {
                match err {
                    AllocError::Disabled => self.mmap_disabled_count.inc(),
                    err => {
                        debug!("failed to mmap allocate: {}", err);
                        self.mmap_error_count.inc();
                    }
                };
                Region::Heap(buf.to_owned())
            }
        };
        let region = self.metrics_region(buf);
        self.alloc_seconds.inc_by(start.elapsed().as_secs_f64());
        region
    }

    /// Wraps the already owned buf into a [Region::Heap] with metrics.
    ///
    /// Besides metrics, this is essentially a no-op.
    pub fn heap_region<T: Copy>(&self, buf: Vec<T>) -> MetricsRegion<T> {
        // Intentionally don't bother incrementing alloc_seconds here.
        self.metrics_region(Region::Heap(buf))
    }

    fn metrics_region<T: Copy>(&self, buf: Region<T>) -> MetricsRegion<T> {
        let metrics = match buf {
            Region::MMap(_) => &self.mmap,
            Region::Heap(_) => &self.heap,
        };
        let region = MetricsRegion {
            buf,
            free_count: metrics.free_count.clone(),
            free_capacity_bytes: metrics.free_capacity_bytes.clone(),
        };
        metrics.alloc_count.inc();
        metrics
            .alloc_capacity_bytes
            .inc_by(u64::cast_from(region.capacity_bytes()));
        let len_bytes = region.buf.len() * std::mem::size_of::<T>();
        self.len_sizes.observe(f64::cast_lossy(len_bytes));
        region
    }
}