From 32988db2bd82edc92f54c2d32fdcbd748ab78cd4 Mon Sep 17 00:00:00 2001 From: Daniel Micay Date: Sun, 11 May 2014 17:41:15 -0400 Subject: [PATCH] heap: add a way to print allocator statistics --- src/libstd/rt/heap.rs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/libstd/rt/heap.rs b/src/libstd/rt/heap.rs index fc62b3f12fb39..b56ba75e38502 100644 --- a/src/libstd/rt/heap.rs +++ b/src/libstd/rt/heap.rs @@ -12,8 +12,9 @@ // FIXME: #13996: need a way to mark the `allocate` and `reallocate` return values as `noalias` use intrinsics::{abort, cttz32}; -use libc::{c_int, c_void, size_t}; -use ptr::RawPtr; +use libc::{c_char, c_int, c_void, size_t}; +use ptr::{RawPtr, mut_null, null}; +use option::{None, Option}; #[link(name = "jemalloc", kind = "static")] extern { @@ -22,6 +23,9 @@ extern { fn je_xallocx(ptr: *mut c_void, size: size_t, extra: size_t, flags: c_int) -> size_t; fn je_dallocx(ptr: *mut c_void, flags: c_int); fn je_nallocx(size: size_t, flags: c_int) -> size_t; + fn je_malloc_stats_print(write_cb: Option, + cbopaque: *mut c_void, + opts: *c_char); } // -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 { unsafe { je_nallocx(size as size_t, mallocx_align(align)) as uint } } +/// Print implementation-defined allocator statistics. +/// +/// These statistics may be inconsistent if other threads use the allocator during the call. +#[unstable] +pub fn stats_print() { + unsafe { + je_malloc_stats_print(None, mut_null(), null()) + } +} + /// The allocator for unique pointers. #[cfg(stage0)] #[lang="exchange_malloc"]