rpprof 0.18.2

An updated, maintained CPU and memory profiling tool for Rust programs.
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
// Copyright 2026 rpprof Authors. Licensed under Apache-2.0 OR MIT.

use std::alloc::{GlobalAlloc, Layout, System};
use std::cell::Cell;
use std::collections::HashMap;
use std::sync::atomic::{AtomicBool, AtomicI64, AtomicU64, AtomicUsize, Ordering};
use std::sync::{Mutex, OnceLock, RwLock};
use std::time::SystemTime;

use crate::Result;
use crate::frames::{Frames, Symbol};
use crate::report::Report;
use crate::timer::{ClockType, ReportTiming};

pub const DEFAULT_SAMPLE_RATE: usize = 512 * 1024; // 512 KiB default sample rate

static IS_PROFILING: AtomicBool = AtomicBool::new(false);
static SAMPLE_RATE: AtomicUsize = AtomicUsize::new(DEFAULT_SAMPLE_RATE);

const NUM_SHARDS: usize = 32;

struct Shard {
    live: HashMap<usize, (u64, usize)>,
}

impl Shard {
    fn new() -> Self {
        Self {
            live: HashMap::new(),
        }
    }
}

struct HeapState {
    shards: Vec<Mutex<Shard>>,
    callsite_map: RwLock<HashMap<Vec<usize>, u64>>,
    callsites: RwLock<HashMap<u64, CallsiteData>>,
    next_callsite_id: AtomicU64,
}

struct CallsiteData {
    ips: Vec<usize>,
    alloc_objects: AtomicU64,
    alloc_bytes: AtomicU64,
    inuse_objects: AtomicI64,
    inuse_bytes: AtomicI64,
}

impl HeapState {
    fn new() -> Self {
        let mut shards = Vec::with_capacity(NUM_SHARDS);
        for _ in 0..NUM_SHARDS {
            shards.push(Mutex::new(Shard::new()));
        }

        Self {
            shards,
            callsite_map: RwLock::new(HashMap::new()),
            callsites: RwLock::new(HashMap::new()),
            next_callsite_id: AtomicU64::new(1),
        }
    }

    fn reset(&self) {
        for shard in &self.shards {
            if let Ok(mut s) = shard.lock() {
                s.live.clear();
            }
        }
        if let Ok(mut map) = self.callsite_map.write() {
            map.clear();
        }
        if let Ok(mut callsites) = self.callsites.write() {
            callsites.clear();
        }
        self.next_callsite_id.store(1, Ordering::SeqCst);
    }
}

static HEAP_STATE: OnceLock<HeapState> = OnceLock::new();

fn state() -> &'static HeapState {
    HEAP_STATE.get_or_init(HeapState::new)
}

thread_local! {
    static IN_ALLOC: Cell<bool> = const { Cell::new(false) };
    static BYTES_LEFT: Cell<isize> = const { Cell::new(0) };
}

/// A sampling heap profiler wrapping any `GlobalAlloc` implementation.
///
/// Can be configured as `#[global_allocator]` to enable low-overhead,
/// Poisson-sampled heap and allocation profiling.
///
/// When profiling is not enabled, runtime overhead is a single atomic check (<1ns).
///
/// # Example
///
/// ```rust,no_run
/// #[global_allocator]
/// static ALLOC: rpprof::alloc::AllocProfiler = rpprof::alloc::AllocProfiler::system();
///
/// fn main() {
///     rpprof::alloc::start();
///
///     // Your workload here...
///
///     let heap_report = rpprof::alloc::heap_report().unwrap();
///     let inuse_report = heap_report.to_inuse_report();
///     let mut file = std::fs::File::create("heap_inuse.folded").unwrap();
///     inuse_report.write_folded(&mut file).unwrap();
///
///     rpprof::alloc::stop();
/// }
/// ```
pub struct AllocProfiler<A = System> {
    inner: A,
}

impl AllocProfiler<System> {
    /// Creates an `AllocProfiler` wrapping the system default allocator.
    pub const fn system() -> Self {
        Self { inner: System }
    }
}

impl<A> AllocProfiler<A> {
    /// Creates an `AllocProfiler` wrapping the provided `GlobalAlloc` allocator.
    pub const fn new(allocator: A) -> Self {
        Self { inner: allocator }
    }
}

impl<A> Default for AllocProfiler<A>
where
    A: Default,
{
    fn default() -> Self {
        Self {
            inner: A::default(),
        }
    }
}

unsafe impl<A: GlobalAlloc> GlobalAlloc for AllocProfiler<A> {
    unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
        let ptr = unsafe { self.inner.alloc(layout) };
        if ptr.is_null() || !IS_PROFILING.load(Ordering::Relaxed) {
            return ptr;
        }

        let _ = IN_ALLOC.try_with(|in_alloc| {
            if in_alloc.get() {
                return;
            }

            let size = layout.size();
            let _ = BYTES_LEFT.try_with(|bytes_left| {
                let remaining = bytes_left.get() - size as isize;
                if remaining <= 0 {
                    in_alloc.set(true);
                    let rate = SAMPLE_RATE.load(Ordering::Relaxed);
                    bytes_left.set(rate as isize);

                    record_sample(ptr as usize, size);
                    in_alloc.set(false);
                } else {
                    bytes_left.set(remaining);
                }
            });
        });

        ptr
    }

    unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
        if IS_PROFILING.load(Ordering::Relaxed) {
            let _ = IN_ALLOC.try_with(|in_alloc| {
                if !in_alloc.get() {
                    in_alloc.set(true);
                    record_dealloc(ptr as usize, layout.size());
                    in_alloc.set(false);
                }
            });
        }

        unsafe { self.inner.dealloc(ptr, layout) };
    }

    unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
        let new_ptr = unsafe { self.inner.realloc(ptr, layout, new_size) };
        if new_ptr.is_null() || !IS_PROFILING.load(Ordering::Relaxed) {
            return new_ptr;
        }

        let _ = IN_ALLOC.try_with(|in_alloc| {
            if !in_alloc.get() {
                in_alloc.set(true);
                record_dealloc(ptr as usize, layout.size());
                record_sample(new_ptr as usize, new_size);
                in_alloc.set(false);
            }
        });

        new_ptr
    }
}

fn record_sample(ptr: usize, size: usize) {
    let mut ips = Vec::with_capacity(32);
    backtrace::trace(|frame| {
        ips.push(frame.ip() as usize);
        ips.len() < crate::MAX_DEPTH
    });

    let s = state();
    let callsite_id = {
        let read = s.callsite_map.read().unwrap();
        if let Some(&id) = read.get(&ips) {
            id
        } else {
            drop(read);
            let mut write = s.callsite_map.write().unwrap();
            *write.entry(ips.clone()).or_insert_with(|| {
                let id = s.next_callsite_id.fetch_add(1, Ordering::SeqCst);
                let mut data_write = s.callsites.write().unwrap();
                data_write.insert(
                    id,
                    CallsiteData {
                        ips,
                        alloc_objects: AtomicU64::new(0),
                        alloc_bytes: AtomicU64::new(0),
                        inuse_objects: AtomicI64::new(0),
                        inuse_bytes: AtomicI64::new(0),
                    },
                );
                id
            })
        }
    };

    if let Ok(data_read) = s.callsites.read()
        && let Some(data) = data_read.get(&callsite_id)
    {
        data.alloc_objects.fetch_add(1, Ordering::Relaxed);
        data.alloc_bytes.fetch_add(size as u64, Ordering::Relaxed);
        data.inuse_objects.fetch_add(1, Ordering::Relaxed);
        data.inuse_bytes.fetch_add(size as i64, Ordering::Relaxed);
    }

    let shard_idx = (ptr >> 4) % NUM_SHARDS;
    if let Ok(mut shard) = s.shards[shard_idx].lock() {
        shard.live.insert(ptr, (callsite_id, size));
    }
}

fn record_dealloc(ptr: usize, _size: usize) {
    let s = state();
    let shard_idx = (ptr >> 4) % NUM_SHARDS;
    let removed = if let Ok(mut shard) = s.shards[shard_idx].lock() {
        shard.live.remove(&ptr)
    } else {
        None
    };

    if let Some((callsite_id, alloc_size)) = removed
        && let Ok(data_read) = s.callsites.read()
        && let Some(data) = data_read.get(&callsite_id)
    {
        data.inuse_objects.fetch_sub(1, Ordering::Relaxed);
        data.inuse_bytes
            .fetch_sub(alloc_size as i64, Ordering::Relaxed);
    }
}

/// Starts heap allocation profiling.
pub fn start() {
    IS_PROFILING.store(true, Ordering::SeqCst);
}

/// Stops heap allocation profiling.
pub fn stop() {
    IS_PROFILING.store(false, Ordering::SeqCst);
}

/// Returns true if heap profiling is currently active.
pub fn is_active() -> bool {
    IS_PROFILING.load(Ordering::Relaxed)
}

/// Resets all recorded heap profile data.
pub fn reset() {
    state().reset();
}

/// Sets the average sampling interval in bytes (default: 512 KiB).
pub fn set_sample_rate(rate_bytes: usize) {
    let rate = if rate_bytes == 0 { 1 } else { rate_bytes };
    SAMPLE_RATE.store(rate, Ordering::SeqCst);
}

/// Returns the current sampling interval in bytes.
pub fn sample_rate() -> usize {
    SAMPLE_RATE.load(Ordering::Relaxed)
}

/// A single callsite entry in a heap profile report.
#[derive(Clone, Debug)]
pub struct HeapRecord {
    pub frames: Frames,
    pub alloc_objects: u64,
    pub alloc_bytes: u64,
    pub inuse_objects: i64,
    pub inuse_bytes: i64,
}

/// A collected heap profile containing allocation and in-use memory records.
#[derive(Clone, Debug)]
pub struct HeapReport {
    pub records: Vec<HeapRecord>,
    pub sample_rate: usize,
    pub timestamp: SystemTime,
}

impl HeapReport {
    /// Total live bytes across all recorded stack traces.
    pub fn total_inuse_bytes(&self) -> i64 {
        self.records.iter().map(|r| r.inuse_bytes.max(0)).sum()
    }

    /// Total cumulative allocated bytes across all recorded stack traces.
    pub fn total_alloc_bytes(&self) -> u64 {
        self.records.iter().map(|r| r.alloc_bytes).sum()
    }

    /// Converts in-use (live) heap allocations into a standard `Report` for flamegraph,
    /// folded stack, or speedscope visualization in bytes.
    pub fn to_inuse_report(&self) -> Report {
        let mut data = HashMap::new();
        for rec in &self.records {
            if rec.inuse_bytes > 0 {
                data.insert(rec.frames.clone(), rec.inuse_bytes as isize);
            }
        }
        Report {
            data,
            timing: ReportTiming {
                frequency: self.sample_rate as i32,
                start_time: self.timestamp,
                duration: std::time::Duration::from_secs(0),
                clock_type: ClockType::Cpu,
            },
            missed_samples: 0,
        }
    }

    /// Converts cumulative allocated heap memory into a standard `Report` for flamegraph,
    /// folded stack, or speedscope visualization in bytes.
    pub fn to_alloc_report(&self) -> Report {
        let mut data = HashMap::new();
        for rec in &self.records {
            if rec.alloc_bytes > 0 {
                data.insert(rec.frames.clone(), rec.alloc_bytes as isize);
            }
        }
        Report {
            data,
            timing: ReportTiming {
                frequency: self.sample_rate as i32,
                start_time: self.timestamp,
                duration: std::time::Duration::from_secs(0),
                clock_type: ClockType::Cpu,
            },
            missed_samples: 0,
        }
    }
}

#[cfg(feature = "_protobuf")]
#[allow(clippy::useless_conversion)]
#[allow(clippy::needless_update)]
mod protobuf {
    use super::*;
    use crate::protos;
    use std::collections::HashSet;

    const ALLOC_OBJECTS: &str = "alloc_objects";
    const ALLOC_SPACE: &str = "alloc_space";
    const INUSE_OBJECTS: &str = "inuse_objects";
    const INUSE_SPACE: &str = "inuse_space";
    const COUNT: &str = "count";
    const BYTES: &str = "bytes";
    const SPACE: &str = "space";

    impl HeapReport {
        /// Generates a standard Google pprof protobuf profile with heap sample types
        /// (alloc_objects, alloc_space, inuse_objects, inuse_space).
        pub fn pprof(&self) -> Result<protos::Profile> {
            let mut dedup_str = HashSet::new();
            for rec in &self.records {
                dedup_str.insert(rec.frames.thread_name_or_id());
                for frame in &rec.frames.frames {
                    for symbol in frame {
                        dedup_str.insert(symbol.name());
                        dedup_str.insert(symbol.sys_name().into_owned());
                        dedup_str.insert(symbol.filename().into_owned());
                    }
                }
            }
            dedup_str.insert(ALLOC_OBJECTS.into());
            dedup_str.insert(ALLOC_SPACE.into());
            dedup_str.insert(INUSE_OBJECTS.into());
            dedup_str.insert(INUSE_SPACE.into());
            dedup_str.insert(COUNT.into());
            dedup_str.insert(BYTES.into());
            dedup_str.insert(SPACE.into());

            let mut str_tbl = vec!["".to_owned()];
            str_tbl.extend(dedup_str);

            let mut strings = HashMap::new();
            for (index, name) in str_tbl.iter().enumerate() {
                strings.insert(name.as_str(), index);
            }

            let mut samples = vec![];
            let mut loc_tbl = vec![];
            let mut fn_tbl = vec![];
            let mut functions = HashMap::new();

            for rec in &self.records {
                let mut locs = vec![];
                for frame in &rec.frames.frames {
                    for symbol in frame {
                        let name = symbol.name();
                        if let Some(loc_idx) = functions.get(&name) {
                            locs.push(*loc_idx);
                            continue;
                        }
                        let sys_name = symbol.sys_name();
                        let filename = symbol.filename();
                        let lineno = symbol.lineno();
                        let function_id = fn_tbl.len() as u64 + 1;
                        let function = protos::Function {
                            id: function_id,
                            name: *strings.get(name.as_str()).unwrap() as i64,
                            system_name: *strings.get(sys_name.as_ref()).unwrap() as i64,
                            filename: *strings.get(filename.as_ref()).unwrap() as i64,
                            ..protos::Function::default()
                        };
                        functions.insert(name, function_id);
                        let line = protos::Line {
                            function_id,
                            line: lineno as i64,
                            ..protos::Line::default()
                        };
                        let loc = protos::Location {
                            id: function_id,
                            line: vec![line].into(),
                            ..protos::Location::default()
                        };
                        fn_tbl.push(function);
                        loc_tbl.push(loc);
                        locs.push(function_id);
                    }
                }

                let sample = protos::Sample {
                    location_id: locs,
                    value: vec![
                        rec.alloc_objects as i64,
                        rec.alloc_bytes as i64,
                        rec.inuse_objects.max(0),
                        rec.inuse_bytes.max(0),
                    ],
                    ..Default::default()
                };
                samples.push(sample);
            }

            let sample_types = vec![
                protos::ValueType {
                    ty: *strings.get(ALLOC_OBJECTS).unwrap() as i64,
                    unit: *strings.get(COUNT).unwrap() as i64,
                    ..Default::default()
                },
                protos::ValueType {
                    ty: *strings.get(ALLOC_SPACE).unwrap() as i64,
                    unit: *strings.get(BYTES).unwrap() as i64,
                    ..Default::default()
                },
                protos::ValueType {
                    ty: *strings.get(INUSE_OBJECTS).unwrap() as i64,
                    unit: *strings.get(COUNT).unwrap() as i64,
                    ..Default::default()
                },
                protos::ValueType {
                    ty: *strings.get(INUSE_SPACE).unwrap() as i64,
                    unit: *strings.get(BYTES).unwrap() as i64,
                    ..Default::default()
                },
            ];

            let period_type = protos::ValueType {
                ty: *strings.get(SPACE).unwrap() as i64,
                unit: *strings.get(BYTES).unwrap() as i64,
                ..Default::default()
            };

            let profile = protos::Profile {
                sample_type: sample_types.into(),
                sample: samples.into(),
                string_table: str_tbl.into(),
                function: fn_tbl.into(),
                location: loc_tbl.into(),
                time_nanos: self
                    .timestamp
                    .duration_since(SystemTime::UNIX_EPOCH)
                    .unwrap_or_default()
                    .as_nanos() as i64,
                period_type: Some(period_type).into(),
                period: self.sample_rate as i64,
                ..protos::Profile::default()
            };

            Ok(profile)
        }
    }
}

/// Takes a snapshot of recorded heap allocations and resolves symbol information.
pub fn heap_report() -> Result<HeapReport> {
    let s = state();
    let data_read = s.callsites.read().unwrap();
    let mut records = Vec::with_capacity(data_read.len());

    for data in data_read.values() {
        let mut frames_vec = Vec::new();

        for &ip in &data.ips {
            let mut symbols = Vec::new();
            backtrace::resolve(ip as *mut _, |sym| {
                symbols.push(Symbol {
                    name: sym.name().map(|n| n.as_bytes().to_vec()),
                    addr: sym.addr(),
                    lineno: sym.lineno(),
                    filename: sym.filename().map(|f| f.to_path_buf()),
                });
            });

            if !symbols.is_empty() {
                frames_vec.push(symbols);
            }
        }

        let frames = Frames {
            frames: frames_vec,
            thread_name: String::new(),
            thread_id: 0,
            sample_timestamp: SystemTime::now(),
        };

        records.push(HeapRecord {
            frames,
            alloc_objects: data.alloc_objects.load(Ordering::Relaxed),
            alloc_bytes: data.alloc_bytes.load(Ordering::Relaxed),
            inuse_objects: data.inuse_objects.load(Ordering::Relaxed),
            inuse_bytes: data.inuse_bytes.load(Ordering::Relaxed),
        });
    }

    Ok(HeapReport {
        records,
        sample_rate: SAMPLE_RATE.load(Ordering::Relaxed),
        timestamp: SystemTime::now(),
    })
}

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

    #[test]
    fn test_alloc_profiler_tracking() {
        reset();
        set_sample_rate(512);

        let profiler = AllocProfiler::system();
        assert_eq!(sample_rate(), 512);

        start();
        assert!(is_active());

        let layout = Layout::from_size_align(1024, 8).unwrap();
        let ptr = unsafe { profiler.alloc(layout) };
        assert!(!ptr.is_null());

        let report = heap_report().unwrap();
        assert!(report.total_alloc_bytes() >= 1024);
        assert!(!report.records.is_empty());

        let inuse_rep = report.to_inuse_report();
        assert!(inuse_rep.total_samples() >= 1024);

        let alloc_rep = report.to_alloc_report();
        assert!(alloc_rep.total_samples() >= 1024);

        #[cfg(feature = "_protobuf")]
        {
            let pb = report.pprof().unwrap();
            assert!(!pb.sample.is_empty());
        }

        unsafe { profiler.dealloc(ptr, layout) };

        stop();
        assert!(!is_active());
        reset();
    }
}