Skip to content

Commit 32988db

Browse files
committed
heap: add a way to print allocator statistics
1 parent f3de28a commit 32988db

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

src/libstd/rt/heap.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
// FIXME: #13996: need a way to mark the `allocate` and `reallocate` return values as `noalias`
1313

1414
use intrinsics::{abort, cttz32};
15-
use libc::{c_int, c_void, size_t};
16-
use ptr::RawPtr;
15+
use libc::{c_char, c_int, c_void, size_t};
16+
use ptr::{RawPtr, mut_null, null};
17+
use option::{None, Option};
1718

1819
#[link(name = "jemalloc", kind = "static")]
1920
extern {
@@ -22,6 +23,9 @@ extern {
2223
fn je_xallocx(ptr: *mut c_void, size: size_t, extra: size_t, flags: c_int) -> size_t;
2324
fn je_dallocx(ptr: *mut c_void, flags: c_int);
2425
fn je_nallocx(size: size_t, flags: c_int) -> size_t;
26+
fn je_malloc_stats_print(write_cb: Option<extern "C" fn(cbopaque: *mut c_void, *c_char)>,
27+
cbopaque: *mut c_void,
28+
opts: *c_char);
2529
}
2630

2731
// -lpthread needs to occur after -ljemalloc, the earlier argument isn't enough
@@ -99,6 +103,16 @@ pub fn usable_size(size: uint, align: uint) -> uint {
99103
unsafe { je_nallocx(size as size_t, mallocx_align(align)) as uint }
100104
}
101105

106+
/// Print implementation-defined allocator statistics.
107+
///
108+
/// These statistics may be inconsistent if other threads use the allocator during the call.
109+
#[unstable]
110+
pub fn stats_print() {
111+
unsafe {
112+
je_malloc_stats_print(None, mut_null(), null())
113+
}
114+
}
115+
102116
/// The allocator for unique pointers.
103117
#[cfg(stage0)]
104118
#[lang="exchange_malloc"]

0 commit comments

Comments
 (0)