From 4c80f4bd8e32fd203bfb5d142ee39fd0518f46e2 Mon Sep 17 00:00:00 2001 From: Ahsan Barkati Date: Mon, 7 Dec 2020 20:23:02 +0530 Subject: [PATCH 1/4] Refresh ctl cache --- contrib/memtest/main.go | 13 +++++++++++-- z/calloc_jemalloc.go | 8 ++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/contrib/memtest/main.go b/contrib/memtest/main.go index a67a3386..c7075e49 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,16 @@ 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(z.NumAllocBytes())), + 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..46a07e59 100644 --- a/z/calloc_jemalloc.go +++ b/z/calloc_jemalloc.go @@ -158,6 +158,14 @@ func ReadMemStats(stats *MemStats) { if stats == nil { return } + 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") From 1f52196f94fae030734175791760602b59992bfa Mon Sep 17 00:00:00 2001 From: Ahsan Barkati Date: Mon, 7 Dec 2020 21:28:01 +0530 Subject: [PATCH 2/4] Refactor --- contrib/memtest/main.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/contrib/memtest/main.go b/contrib/memtest/main.go index c7075e49..4cb8335d 100644 --- a/contrib/memtest/main.go +++ b/contrib/memtest/main.go @@ -114,10 +114,8 @@ func memory() { 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(z.NumAllocBytes())), - increase, + " Resident: %s, Retained: %s]\n", + counter, humanize.IBytes(uint64(z.NumAllocBytes())), increase, humanize.IBytes(js.Active), humanize.IBytes(js.Allocated), humanize.IBytes(js.Resident), humanize.IBytes(js.Retained)) } From 86240440f4b11f82d2a5e517195fe3fc503ac7fe Mon Sep 17 00:00:00 2001 From: Ahsan Barkati Date: Mon, 7 Dec 2020 22:40:17 +0530 Subject: [PATCH 3/4] Add comment --- z/calloc_jemalloc.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/z/calloc_jemalloc.go b/z/calloc_jemalloc.go index 46a07e59..f37f6753 100644 --- a/z/calloc_jemalloc.go +++ b/z/calloc_jemalloc.go @@ -158,6 +158,10 @@ 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( From 1bef3baa5c61a7a51cd39b49ac942dfb63b24b31 Mon Sep 17 00:00:00 2001 From: Ahsan Barkati Date: Mon, 7 Dec 2020 23:08:11 +0530 Subject: [PATCH 4/4] Minor fix --- contrib/memtest/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contrib/memtest/main.go b/contrib/memtest/main.go index 4cb8335d..c88734a3 100644 --- a/contrib/memtest/main.go +++ b/contrib/memtest/main.go @@ -115,7 +115,7 @@ func memory() { fmt.Printf("[%d] Current Memory: %s. Increase? %v, MemStats [Active: %s, Allocated: %s,"+ " Resident: %s, Retained: %s]\n", - counter, humanize.IBytes(uint64(z.NumAllocBytes())), increase, + counter, humanize.IBytes(uint64(curMem)), increase, humanize.IBytes(js.Active), humanize.IBytes(js.Allocated), humanize.IBytes(js.Resident), humanize.IBytes(js.Retained)) }