From: Wang Deyu Date: Fri, 16 Sep 2022 10:56:48 +0000 (+0800) Subject: runtime: avoid initializing MemProfileRate in init function X-Git-Tag: go1.20rc1~729 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=f63b268b9a1dd6457405ed24dd4831b6ebfb8df8;p=gostls13.git runtime: avoid initializing MemProfileRate in init function Fixes #55100 Change-Id: Ibbff921e74c3a416fd8bb019d20410273961c015 Reviewed-on: https://go-review.googlesource.com/c/go/+/431315 Auto-Submit: Michael Pratt TryBot-Result: Gopher Robot Run-TryBot: Michael Pratt Reviewed-by: Cherry Mui Reviewed-by: Michael Pratt --- diff --git a/src/runtime/mprof.go b/src/runtime/mprof.go index 8cef0b0601..4134a961d6 100644 --- a/src/runtime/mprof.go +++ b/src/runtime/mprof.go @@ -584,17 +584,7 @@ func (r *StackRecord) Stack() []uintptr { // memory profiling rate should do so just once, as early as // possible in the execution of the program (for example, // at the beginning of main). -var MemProfileRate int = defaultMemProfileRate(512 * 1024) - -// defaultMemProfileRate returns 0 if disableMemoryProfiling is set. -// It exists primarily for the godoc rendering of MemProfileRate -// above. -func defaultMemProfileRate(v int) int { - if disableMemoryProfiling { - return 0 - } - return v -} +var MemProfileRate int = 512 * 1024 // disableMemoryProfiling is set by the linker if runtime.MemProfile // is not used and the link type guarantees nobody else could use it diff --git a/src/runtime/proc.go b/src/runtime/proc.go index 596778718a..02390375b5 100644 --- a/src/runtime/proc.go +++ b/src/runtime/proc.go @@ -723,6 +723,14 @@ func schedinit() { parsedebugvars() gcinit() + // if disableMemoryProfiling is set, update MemProfileRate to 0 to turn off memprofile. + // Note: parsedebugvars may update MemProfileRate, but when disableMemoryProfiling is + // set to true by the linker, it means that nothing is consuming the profile, it is + // safe to set MemProfileRate to 0. + if disableMemoryProfiling { + MemProfileRate = 0 + } + lock(&sched.lock) sched.lastpoll.Store(nanotime()) procs := ncpu