-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdhat_json.rs
More file actions
71 lines (62 loc) · 1.85 KB
/
Copy pathdhat_json.rs
File metadata and controls
71 lines (62 loc) · 1.85 KB
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
//! DHAT-compatible JSON output demo.
//!
//! Run with:
//! cargo run --release --features dhat-compat --example dhat_json
//!
//! Or with symbolicated frames in the JSON:
//! cargo run --release --features symbolicate,dhat-compat --example dhat_json
//!
//! Drops a `dhat-heap.json` file in the current working directory.
//! Load that file in `dh_view.html` (shipped with Valgrind) to
//! inspect the per-call-site report visually.
#[cfg(feature = "dhat-compat")]
#[global_allocator]
static GLOBAL: mod_alloc::ModAlloc = mod_alloc::ModAlloc::new();
#[cfg(feature = "dhat-compat")]
#[inline(never)]
fn alloc_small() {
let v: Vec<u8> = Vec::with_capacity(64);
std::hint::black_box(&v);
}
#[cfg(feature = "dhat-compat")]
#[inline(never)]
fn alloc_medium() {
let v: Vec<u8> = Vec::with_capacity(1024);
std::hint::black_box(&v);
}
#[cfg(feature = "dhat-compat")]
#[inline(never)]
fn alloc_large() {
let v: Vec<u8> = Vec::with_capacity(64 * 1024);
std::hint::black_box(&v);
}
#[cfg(feature = "dhat-compat")]
fn main() -> std::io::Result<()> {
for _ in 0..500 {
alloc_small();
}
for _ in 0..100 {
alloc_medium();
}
for _ in 0..10 {
alloc_large();
}
let snap = GLOBAL.snapshot();
println!(
"captured {} allocations, {} total bytes, peak {} bytes",
snap.alloc_count, snap.total_bytes, snap.peak_bytes
);
let path = std::path::Path::new("dhat-heap.json");
GLOBAL.write_dhat_json(path)?;
let abs = std::fs::canonicalize(path).unwrap_or_else(|_| path.to_path_buf());
println!("wrote {}", abs.display());
println!("open it in dh_view.html to inspect.");
Ok(())
}
#[cfg(not(feature = "dhat-compat"))]
fn main() {
eprintln!(
"this example requires the `dhat-compat` feature; run with \
`cargo run --features dhat-compat --example dhat_json`"
);
}