-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathalloc_profiler.rs
37 lines (29 loc) · 1.03 KB
/
alloc_profiler.rs
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
use {
std::time::Duration,
wc::alloc::{
profiler::{self, JemallocMultiBinFilter},
Jemalloc,
},
};
/// Configure profiler allocator to track specific allocation bins (4096 and
/// 8192 bytes).
#[global_allocator]
static ALLOCATOR: profiler::Alloc<Jemalloc, JemallocMultiBinFilter<2>> =
profiler::Alloc::new(Jemalloc, JemallocMultiBinFilter::new([4096, 8192]));
fn allocate(capacity: usize) -> Vec<u8> {
Vec::<u8>::with_capacity(capacity)
}
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let handle = tokio::spawn(profiler::record(Duration::from_millis(500)));
// Give some time for tokio to actually execute the profiler future.
tokio::time::sleep(Duration::from_millis(100)).await;
// Allocate memory.
let mut _buffer = allocate(256); // This one should not be recorded.
let mut _buffer = allocate(4096);
let mut _buffer = allocate(8192);
// Obtain JSON-serialized DHAT profile.
let profile = handle.await.unwrap().unwrap();
eprintln!("{profile}");
Ok(())
}