]> Cypherpunks.ru repositories - gostls13.git/commitdiff
runtime: avoid initializing MemProfileRate in init function
authorWang Deyu <wangdeyu.2021@bytedance.com>
Fri, 16 Sep 2022 10:56:48 +0000 (18:56 +0800)
committerGopher Robot <gobot@golang.org>
Wed, 5 Oct 2022 17:31:56 +0000 (17:31 +0000)
Fixes #55100

Change-Id: Ibbff921e74c3a416fd8bb019d20410273961c015
Reviewed-on: https://go-review.googlesource.com/c/go/+/431315
Auto-Submit: Michael Pratt <mpratt@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Michael Pratt <mpratt@google.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
Reviewed-by: Michael Pratt <mpratt@google.com>
src/runtime/mprof.go
src/runtime/proc.go

index 8cef0b0601a03e04c8433a051d81263ea074b869..4134a961d6aaf2117898399ea8adb023cb6b4dca 100644 (file)
@@ -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
index 596778718aa5352cfb18c19fe5b4fd0ea2ad2cdd..02390375b5a77ba83645a2efbbb20f58fa73a43b 100644 (file)
@@ -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