libdd_profiling_heap_allocator/lib.rs
1// Copyright 2025-Present Datadog, Inc. https://www.datadoghq.com/
2// SPDX-License-Identifier: Apache-2.0
3
4//! Rust `GlobalAlloc` wrapper that drives `libdd-profiling-heap-sampler` around each
5//! allocation. Wrap any underlying allocator with [`SampledAllocator`]; on
6//! each alloc/dealloc the sampler's decision/flag/USDT machinery runs
7//! around the inner call.
8//!
9//! `SampledAllocator` is portable across targets, so callers can use it in
10//! a single `#[global_allocator]` static without cfg-gating. The sampling
11//! integration (USDT probes via `libdd-profiling-heap-sampler`) is Linux-only; on
12//! every other target the wrapper compiles to a transparent pass-through
13//! to the inner allocator.
14//!
15//! # Features
16//!
17//! * `live-heap` (off by default) - enables live-heap tracking: sampled allocations are flagged at
18//! alloc time, and that flag is detected again on free, so a profiler can pair each free back to
19//! its sample and balance allocs against frees. Off = allocation profiling only.
20//!
21//! # Example
22//!
23//! ```no_run
24//! use libdd_profiling_heap_allocator::{
25//! is_profiler_attached, set_default_sampling_distance, SampledAllocator,
26//! };
27//! use std::alloc::System;
28//!
29//! #[global_allocator]
30//! static ALLOC: SampledAllocator<System> = SampledAllocator::<System>::DEFAULT;
31//!
32//! fn main() {
33//! // Configure the mean sample distance before the application's
34//! // allocation-heavy work begins. New threads pick up this value
35//! // when their sampler state is first initialized.
36//! set_default_sampling_distance(256 * 1024);
37//!
38//! // ... application runs ...
39//! }
40//! ```
41
42mod allocator;
43
44pub use allocator::SampledAllocator;
45
46/// Returns true when an external profiler is currently attached to the
47/// allocator's `ddheap:alloc` USDT.
48///
49/// This is a point-in-time read of the USDT semaphore. A profiler can attach
50/// or detach immediately after this returns. Use it as a diagnostic/readiness
51/// signal, not as a synchronization primitive.
52#[inline]
53pub fn is_profiler_attached() -> bool {
54 libdd_profiling_heap_sampler::is_profiler_attached()
55}
56
57/// Set the default mean sample distance (bytes between samples) for the
58/// heap sampler.
59///
60/// The sampler draws from an exponential distribution around this mean,
61/// so individual gaps vary but average to the configured value. Pass `0`
62/// to revert to the compiled-in default (512 KiB). Values below 64 KiB
63/// are clamped to 64 KiB to avoid excessive overhead.
64///
65/// Call this at the top of `main`, before the application's
66/// allocation-heavy work begins. Threads that have already initialized
67/// their sampler state will not pick up the new value.
68#[cfg(target_os = "linux")]
69pub fn set_default_sampling_distance(distance_bytes: u64) {
70 libdd_profiling_heap_sampler::set_default_sampling_distance(distance_bytes);
71}
72
73/// See the Linux variant above.
74#[cfg(not(target_os = "linux"))]
75pub fn set_default_sampling_distance(_distance_bytes: u64) {}