diff --git a/contrib/memtest/main.go b/contrib/memtest/main.go index a67a3386..c88734a3 100644 --- a/contrib/memtest/main.go +++ b/contrib/memtest/main.go @@ -33,6 +33,7 @@ import ( "unsafe" "github.com/dgraph-io/ristretto/z" + "github.com/dustin/go-humanize" ) type S struct { @@ -109,8 +110,14 @@ func memory() { counter++ } } - fmt.Printf("[%d] Current Memory: %05.2f G. Increase? %v\n", - counter, float64(curMem)/float64(1<<30), increase) + var js z.MemStats + z.ReadMemStats(&js) + + fmt.Printf("[%d] Current Memory: %s. Increase? %v, MemStats [Active: %s, Allocated: %s,"+ + " Resident: %s, Retained: %s]\n", + counter, humanize.IBytes(uint64(curMem)), increase, + humanize.IBytes(js.Active), humanize.IBytes(js.Allocated), + humanize.IBytes(js.Resident), humanize.IBytes(js.Retained)) } func viaLL() { diff --git a/z/calloc_jemalloc.go b/z/calloc_jemalloc.go index f4a19317..f37f6753 100644 --- a/z/calloc_jemalloc.go +++ b/z/calloc_jemalloc.go @@ -158,6 +158,18 @@ func ReadMemStats(stats *MemStats) { if stats == nil { return } + // Call an epoch mallclt to refresh the stats data as mentioned in the docs. + // http://jemalloc.net/jemalloc.3.html#epoch + // Note: This epoch mallctl is as expensive as a malloc call. It takes up the + // malloc_mutex_lock. + epoch := 1 + sz := unsafe.Sizeof(&epoch) + C.je_mallctl( + (C.CString)("epoch"), + unsafe.Pointer(&epoch), + (*C.size_t)(unsafe.Pointer(&sz)), + unsafe.Pointer(&epoch), + (C.size_t)(unsafe.Sizeof(epoch))) stats.Allocated = fetchStat("stats.allocated") stats.Active = fetchStat("stats.active") stats.Resident = fetchStat("stats.resident")