From: Raul Silvera Date: Fri, 18 Jan 2019 00:14:29 +0000 (+0000) Subject: runtime: sample large heap allocations correctly X-Git-Tag: go1.12rc1~59 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=dc889025c714313fbe912ad1253f0ec748a6e58c;p=gostls13.git runtime: sample large heap allocations correctly Remove an unnecessary check on the heap sampling code that forced sampling of all heap allocations larger than the sampling rate. This need to follow a poisson process so that they can be correctly unsampled. Maintain a check for MemProfileRate==1 to provide a mechanism for full sampling, as documented in https://golang.org/pkg/runtime/#pkg-variables. Additional testing for this change is on cl/129117. Fixes #26618 Change-Id: I7802bde2afc655cf42cffac34af9bafeb3361957 GitHub-Last-Rev: 471f747af845395d458096bea26daa93b91120be GitHub-Pull-Request: golang/go#29791 Reviewed-on: https://go-review.googlesource.com/c/158337 Run-TryBot: Austin Clements TryBot-Result: Gobot Gobot Reviewed-by: Hyang-Ah Hana Kim --- diff --git a/src/runtime/malloc.go b/src/runtime/malloc.go index c1a89dc588..8c617bb42b 100644 --- a/src/runtime/malloc.go +++ b/src/runtime/malloc.go @@ -1012,7 +1012,7 @@ func mallocgc(size uintptr, typ *_type, needzero bool) unsafe.Pointer { } if rate := MemProfileRate; rate > 0 { - if size < uintptr(rate) && int32(size) < c.next_sample { + if rate != 1 && int32(size) < c.next_sample { c.next_sample -= int32(size) } else { mp := acquirem() diff --git a/src/runtime/testdata/testprog/memprof.go b/src/runtime/testdata/testprog/memprof.go index a22fee61d7..7b134bc078 100644 --- a/src/runtime/testdata/testprog/memprof.go +++ b/src/runtime/testdata/testprog/memprof.go @@ -21,7 +21,10 @@ var memProfBuf bytes.Buffer var memProfStr string func MemProf() { - for i := 0; i < 1000; i++ { + // Force heap sampling for determinism. + runtime.MemProfileRate = 1 + + for i := 0; i < 10; i++ { fmt.Fprintf(&memProfBuf, "%*d\n", i, i) } memProfStr = memProfBuf.String()