From 7da03b9fbb5c0f8b771a90be3c3777ffbdde283a Mon Sep 17 00:00:00 2001 From: Austin Clements Date: Wed, 21 Jun 2017 12:11:52 -0400 Subject: [PATCH] runtime: compute goal first in gcSetTriggerRatio This slightly rearranges gcSetTriggerRatio to compute the goal before computing the other controls. This will simplify implementing the heap limit, which needs to control the absolute goal and flow the rest of the control parameters from this. For #16843. Change-Id: I46b7c1f8b6e4edbee78930fb093b60bd1a03d75e Reviewed-on: https://go-review.googlesource.com/c/go/+/46750 Run-TryBot: Austin Clements Reviewed-by: Michael Knyszek Reviewed-by: Rick Hudson --- src/runtime/mgc.go | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/runtime/mgc.go b/src/runtime/mgc.go index 5b974d466b..730b64cd19 100644 --- a/src/runtime/mgc.go +++ b/src/runtime/mgc.go @@ -765,6 +765,14 @@ func pollFractionalWorkerExit() bool { // // mheap_.lock must be held or the world must be stopped. func gcSetTriggerRatio(triggerRatio float64) { + // Compute the next GC goal, which is when the allocated heap + // has grown by GOGC/100 over the heap marked by the last + // cycle. + goal := ^uint64(0) + if gcpercent >= 0 { + goal = memstats.heap_marked + memstats.heap_marked*uint64(gcpercent)/100 + } + // Set the trigger ratio, capped to reasonable bounds. if triggerRatio < 0 { // This can happen if the mutator is allocating very @@ -807,22 +815,16 @@ func gcSetTriggerRatio(triggerRatio float64) { print("runtime: next_gc=", memstats.next_gc, " heap_marked=", memstats.heap_marked, " heap_live=", memstats.heap_live, " initialHeapLive=", work.initialHeapLive, "triggerRatio=", triggerRatio, " minTrigger=", minTrigger, "\n") throw("gc_trigger underflow") } - } - memstats.gc_trigger = trigger - - // Compute the next GC goal, which is when the allocated heap - // has grown by GOGC/100 over the heap marked by the last - // cycle. - goal := ^uint64(0) - if gcpercent >= 0 { - goal = memstats.heap_marked + memstats.heap_marked*uint64(gcpercent)/100 - if goal < trigger { + if trigger > goal { // The trigger ratio is always less than GOGC/100, but // other bounds on the trigger may have raised it. // Push up the goal, too. goal = trigger } } + + // Commit to the trigger and goal. + memstats.gc_trigger = trigger memstats.next_gc = goal if trace.enabled { traceNextGC() -- 2.50.0