stoolap-jemalloc 0.1.1

A jemalloc-style memory allocator written in pure Rust, with a built-in pprof heap profiler
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
// Copyright 2026 Stoolap Contributors
//
// 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.

//! Encodes the profile as pprof's `profile.proto`, by hand to stay free
//! of dependencies

use super::StackCounts;
use super::maps::{self, Mapping};
use std::collections::HashMap;
use std::fmt;

/// Why a heap profile could not be dumped
#[derive(Debug)]
pub enum DumpError {
    /// Profiling was never activated, so there is nothing to dump
    NotActivated,
    /// Writing the profile file failed
    Io(std::io::Error),
}

impl fmt::Display for DumpError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            DumpError::NotActivated => f.write_str("heap profiling was never activated"),
            DumpError::Io(e) => write!(f, "writing the heap profile: {e}"),
        }
    }
}

impl std::error::Error for DumpError {}

// Wire types
const VARINT: u64 = 0;
const LEN: u64 = 2;

fn varint(b: &mut Vec<u8>, mut v: u64) {
    while v >= 0x80 {
        b.push(v as u8 | 0x80);
        v >>= 7;
    }
    b.push(v as u8);
}

fn tag(b: &mut Vec<u8>, field: u64, wire: u64) {
    varint(b, field << 3 | wire);
}

fn uint(b: &mut Vec<u8>, field: u64, v: u64) {
    if v != 0 {
        tag(b, field, VARINT);
        varint(b, v);
    }
}

fn bytes(b: &mut Vec<u8>, field: u64, data: &[u8]) {
    tag(b, field, LEN);
    varint(b, data.len() as u64);
    b.extend_from_slice(data);
}

fn packed(b: &mut Vec<u8>, field: u64, values: impl IntoIterator<Item = u64>) {
    let mut tmp = Vec::new();
    for v in values {
        varint(&mut tmp, v);
    }
    bytes(b, field, &tmp);
}

#[derive(Default)]
struct Strings {
    ids: HashMap<String, u64>,
    list: Vec<String>,
}

impl Strings {
    fn id(&mut self, s: &str) -> u64 {
        if self.list.is_empty() {
            self.list.push(String::new());
            self.ids.insert(String::new(), 0);
        }
        if let Some(&id) = self.ids.get(s) {
            return id;
        }
        let id = self.list.len() as u64;
        self.list.push(s.to_string());
        self.ids.insert(s.to_string(), id);
        id
    }
}

/// `ValueType { type, unit }`
fn value_type(strings: &mut Strings, ty: &str, unit: &str) -> Vec<u8> {
    let mut b = Vec::new();
    uint(&mut b, 1, strings.id(ty));
    uint(&mut b, 2, strings.id(unit));
    b
}

/// A source line of a location: the function's name, simplified for
/// display and in full, its file and the line number
struct Line {
    name: String,
    system_name: String,
    file: String,
    number: u64,
}

#[cfg(feature = "symbolize")]
/// Rust's demangled name without generic arguments and trait
/// qualifications, which pprof would otherwise cut into pieces:
/// `<core::result::Result<T, E> as FnOnce<A>>::call_once` becomes
/// `core::result::Result::call_once`
fn simplify(name: &str) -> String {
    enum Bracket {
        Generic,
        Qualified { trait_part: bool },
    }
    let mut out = String::with_capacity(name.len());
    let mut stack: Vec<Bracket> = Vec::new();
    let mut rest = name;
    let mut prev = ' ';
    while let Some(c) = rest.chars().next() {
        let skipping = stack.iter().any(|b| {
            matches!(
                b,
                Bracket::Generic | Bracket::Qualified { trait_part: true }
            )
        });
        match c {
            '<' => {
                // `Vec<T>` and `f::<T>` are generic; `<impl Trait for T>` is not
                let generic = skipping
                    || (!rest.starts_with("<impl ")
                        && out.ends_with(|p: char| p.is_alphanumeric() || p == '_' || p == ':'));
                if generic && !skipping && out.ends_with("::") {
                    out.truncate(out.len() - 2);
                }
                stack.push(if generic {
                    Bracket::Generic
                } else {
                    Bracket::Qualified { trait_part: false }
                });
            }
            // `->` inside generic arguments is not a closing bracket
            '>' if !stack.is_empty() && prev != '-' => {
                stack.pop();
            }
            _ if skipping => {}
            ' ' if rest.starts_with(" as ")
                && matches!(stack.last(), Some(Bracket::Qualified { .. })) =>
            {
                stack.pop();
                stack.push(Bracket::Qualified { trait_part: true });
                rest = &rest[" as".len()..];
            }
            _ => out.push(c),
        }
        rest = &rest[c.len_utf8()..];
        prev = c;
    }
    out
}

/// Whether a line belongs to the allocator itself: by its source file,
/// or by its name, for the allocator shims that live in the program.
/// Builds with `debug = "line-tables-only"` name functions without their
/// paths, so the name alone does not tell.
fn is_internal(line: &Line) -> bool {
    let own_source = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("src");
    std::path::Path::new(&line.file).starts_with(own_source)
        || ["stoolap_jemalloc::", "__rustc::", "__rust_", "__rg_"]
            .iter()
            .any(|p| line.name.starts_with(p))
}

/// A location's lines without the allocator's own, which inlining puts at
/// the innermost end; `None` when every line is the allocator's
fn user_lines(addr: usize) -> Option<Vec<Line>> {
    let mut lines = symbolize(addr);
    let internal = lines.iter().take_while(|l| is_internal(l)).count();
    if internal > 0 && internal == lines.len() {
        return None;
    }
    lines.drain(..internal);
    Some(lines)
}

#[cfg(feature = "symbolize")]
fn symbolize(addr: usize) -> Vec<Line> {
    let mut lines = Vec::new();
    // Innermost first, as pprof wants inlined frames ordered
    backtrace::resolve(addr as *mut core::ffi::c_void, |sym| {
        let Some(name) = sym.name() else { return };
        let system_name = format!("{name:#}");
        lines.push(Line {
            name: simplify(&system_name),
            system_name,
            file: sym
                .filename()
                .map(|f| f.display().to_string())
                .unwrap_or_default(),
            number: u64::from(sym.lineno().unwrap_or(0)),
        });
    });
    lines
}

#[cfg(not(feature = "symbolize"))]
fn symbolize(_addr: usize) -> Vec<Line> {
    Vec::new()
}

pub(super) fn encode(stacks: &[StackCounts], interval: usize) -> Vec<u8> {
    let mut strings = Strings::default();
    strings.id("");
    let mut out = Vec::new();

    for (ty, unit) in [
        ("alloc_objects", "count"),
        ("alloc_space", "bytes"),
        ("inuse_objects", "count"),
        ("inuse_space", "bytes"),
    ] {
        let vt = value_type(&mut strings, ty, unit);
        bytes(&mut out, 1, &vt);
    }

    let mut mappings = maps::mappings();
    mappings.sort_by_key(|m| m.start);
    let mapping_of = |addr: u64| -> u64 {
        let i = mappings.partition_point(|m| m.start <= addr);
        if i > 0 && addr < mappings[i - 1].limit {
            i as u64
        } else {
            0
        }
    };

    let mut symbols: HashMap<usize, Option<Vec<Line>>> = HashMap::new();
    let mut locations = Locations::default();
    // Whether every location in each mapping got symbols; the symbolizer
    // misses libraries loaded after its first use, and those are left for
    // pprof to symbolize
    let mut symbolized = vec![cfg!(feature = "symbolize"); mappings.len() + 1];

    for s in stacks {
        if s.alloc_objects == 0 {
            continue;
        }
        let mut ids = Vec::with_capacity(s.frames.len());
        for &addr in s.frames {
            let Some(lines) = symbols.entry(addr).or_insert_with(|| user_lines(addr)) else {
                // The allocator's frames are only at the top of a stack
                continue;
            };
            let mapping = mapping_of(addr as u64);
            if lines.is_empty() {
                symbolized[mapping as usize] = false;
            }
            ids.push(locations.id(addr, lines, mapping, &mut strings));
        }
        let mut sample = Vec::new();
        packed(&mut sample, 1, ids);
        packed(
            &mut sample,
            2,
            [
                s.alloc_objects,
                s.alloc_bytes,
                s.inuse_objects,
                s.inuse_bytes,
            ],
        );
        bytes(&mut out, 2, &sample);
    }

    for (i, m) in mappings.iter().enumerate() {
        let complete = u64::from(symbolized[i + 1]);
        out.extend(encode_mapping(&mut strings, i as u64 + 1, m, complete));
    }
    out.extend(locations.encoded);
    out.extend(locations.functions_encoded);

    let now = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map_or(0, |d| d.as_nanos() as u64);
    uint(&mut out, 9, now);
    let period_type = value_type(&mut strings, "space", "bytes");
    bytes(&mut out, 11, &period_type);
    uint(&mut out, 12, interval as u64);
    let default = strings.id("inuse_space");
    uint(&mut out, 14, default);

    for s in &strings.list {
        bytes(&mut out, 6, s.as_bytes());
    }
    out
}

/// Locations and the functions their lines name, each encoded once
#[derive(Default)]
struct Locations {
    ids: HashMap<usize, u64>,
    /// By full name and file: simplified names of different functions,
    /// such as two traits' methods on one type, can be the same
    functions: HashMap<(String, String), u64>,
    encoded: Vec<u8>,
    functions_encoded: Vec<u8>,
}

impl Locations {
    /// The id of the location at `addr`, encoding it on first use
    fn id(&mut self, addr: usize, lines: &[Line], mapping: u64, strings: &mut Strings) -> u64 {
        if let Some(&id) = self.ids.get(&addr) {
            return id;
        }
        let id = self.ids.len() as u64 + 1;
        self.ids.insert(addr, id);
        let mut loc = Vec::new();
        uint(&mut loc, 1, id);
        uint(&mut loc, 2, mapping);
        uint(&mut loc, 3, addr as u64);
        for line in lines {
            let mut l = Vec::new();
            uint(&mut l, 1, self.function(line, strings));
            uint(&mut l, 2, line.number);
            bytes(&mut loc, 4, &l);
        }
        bytes(&mut self.encoded, 4, &loc);
        id
    }

    fn function(&mut self, line: &Line, strings: &mut Strings) -> u64 {
        let key = (line.system_name.clone(), line.file.clone());
        if let Some(&id) = self.functions.get(&key) {
            return id;
        }
        let id = self.functions.len() as u64 + 1;
        let mut f = Vec::new();
        uint(&mut f, 1, id);
        uint(&mut f, 2, strings.id(&line.name));
        uint(&mut f, 3, strings.id(&line.system_name));
        uint(&mut f, 4, strings.id(&line.file));
        bytes(&mut self.functions_encoded, 5, &f);
        self.functions.insert(key, id);
        id
    }
}

/// A mapping; `symbolized` says whether its locations carry their
/// functions and lines already
fn encode_mapping(strings: &mut Strings, id: u64, m: &Mapping, symbolized: u64) -> Vec<u8> {
    let mut b = Vec::new();
    uint(&mut b, 1, id);
    uint(&mut b, 2, m.start);
    uint(&mut b, 3, m.limit);
    uint(&mut b, 4, m.offset);
    uint(&mut b, 5, strings.id(&m.path));
    for field in 7..=10 {
        uint(&mut b, field, symbolized);
    }
    let mut out = Vec::new();
    bytes(&mut out, 3, &b);
    out
}

#[cfg(all(test, feature = "symbolize"))]
mod tests {
    use super::{Line, is_internal, simplify};

    fn line(name: &str, file: &str) -> Line {
        Line {
            name: name.to_string(),
            system_name: name.to_string(),
            file: file.to_string(),
            number: 1,
        }
    }

    /// With line tables only, names come without paths: the allocator's own
    /// frames are still told by their files
    #[test]
    fn internal_frames_without_paths() {
        let own = format!("{}/src/malloc.rs", env!("CARGO_MANIFEST_DIR"));
        assert!(is_internal(&line("alloc", &own)));
        assert!(is_internal(&line("stoolap_jemalloc::malloc::alloc", "")));
        assert!(is_internal(&line("__rust_alloc", "/app/src/lib.rs")));
        assert!(!is_internal(&line(
            "alloc",
            "/rustlib/src/rust/library/alloc/src/alloc.rs"
        )));
        assert!(!is_internal(&line(
            "add_versions_batch",
            "/app/src/storage/mvcc.rs"
        )));
    }

    #[test]
    fn simplifies_rust_names() {
        let cases = [
            ("alloc::raw_vec::finish_grow", "alloc::raw_vec::finish_grow"),
            (
                "<alloc::raw_vec::RawVec<T,A>>::finish_grow",
                "alloc::raw_vec::RawVec::finish_grow",
            ),
            (
                "<core::result::Result<T, E> as core::ops::function::FnOnce<A>>::call_once",
                "core::result::Result::call_once",
            ),
            ("<&T as core::fmt::Display>::fmt", "&T::fmt"),
            (
                "std::thread::Builder::spawn::{{closure}}",
                "std::thread::Builder::spawn::{{closure}}",
            ),
            ("foo::<impl Fn() -> u8>::bar", "foo::impl Fn() -> u8::bar"),
            (
                "RawVecInner::reserve::do_reserve_and_handle::<alloc::alloc::Global>",
                "RawVecInner::reserve::do_reserve_and_handle",
            ),
            (
                "core::ptr::drop_in_place::<alloc::vec::Vec<u8>>",
                "core::ptr::drop_in_place",
            ),
        ];
        for (name, want) in cases {
            assert_eq!(simplify(name), want, "{name}");
        }
    }
}