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
// Copyright 2025-Present Datadog, Inc. https://www.datadoghq.com/
// SPDX-License-Identifier: Apache-2.0
//! Rust `GlobalAlloc` wrapper that drives `libdd-profiling-heap-sampler` around each
//! allocation. Wrap any underlying allocator with [`SampledAllocator`]; on
//! each alloc/dealloc the sampler's decision/flag/USDT machinery runs
//! around the inner call.
//!
//! `SampledAllocator` is portable across targets, so callers can use it in
//! a single `#[global_allocator]` static without cfg-gating. The sampling
//! integration (USDT probes via `libdd-profiling-heap-sampler`) is Linux-only; on
//! every other target the wrapper compiles to a transparent pass-through
//! to the inner allocator.
//!
//! # Features
//!
//! * `live-heap` (off by default) - enables live-heap tracking: sampled allocations are flagged at
//! alloc time, and that flag is detected again on free, so a profiler can pair each free back to
//! its sample and balance allocs against frees. Off = allocation profiling only.
//!
//! # Example
//!
//! ```no_run
//! use libdd_profiling_heap_allocator::{set_default_sampling_distance, SampledAllocator};
//! use std::alloc::System;
//!
//! #[global_allocator]
//! static ALLOC: SampledAllocator<System> = SampledAllocator::<System>::DEFAULT;
//!
//! fn main() {
//! // Configure the mean sample distance before the application's
//! // allocation-heavy work begins. New threads pick up this value
//! // when their sampler state is first initialized.
//! set_default_sampling_distance(256 * 1024);
//!
//! // ... application runs ...
//! }
//! ```
pub use SampledAllocator;
/// Set the default mean sample distance (bytes between samples) for the
/// heap sampler.
///
/// The sampler draws from an exponential distribution around this mean,
/// so individual gaps vary but average to the configured value. Pass `0`
/// to revert to the compiled-in default (512 KiB). Values below 64 KiB
/// are clamped to 64 KiB to avoid excessive overhead.
///
/// Call this at the top of `main`, before the application's
/// allocation-heavy work begins. Threads that have already initialized
/// their sampler state will not pick up the new value.
/// See the Linux variant above.