Skip to content

Commit 9d7dad1

Browse files
mknyszekianlancetaylor
authored andcommitted
[release-branch.go1.14] runtime: ensure minTriggerRatio never exceeds maxTriggerRatio
Currently, the capping logic for the GC trigger ratio is such that if gcpercent is low, we may end up setting the trigger ratio far too high, breaking the promise of SetGCPercent and GOGC has a trade-off knob (we won't start a GC early enough, and we will use more memory). This change modifies the capping logic for the trigger ratio by scaling the minTriggerRatio with gcpercent the same way we scale maxTriggerRatio. For #37927. Fixes #37928. Change-Id: I2a048c1808fb67186333d3d5a6bee328be2f35da Reviewed-on: https://go-review.googlesource.com/c/go/+/223937 Run-TryBot: Michael Knyszek <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Austin Clements <[email protected]> (cherry picked from commit d1ecfcc) Reviewed-on: https://go-review.googlesource.com/c/go/+/225637 Reviewed-by: David Chase <[email protected]>
1 parent 612ef03 commit 9d7dad1

File tree

1 file changed

+28
-20
lines changed

1 file changed

+28
-20
lines changed

src/runtime/mgc.go

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -769,32 +769,40 @@ func gcSetTriggerRatio(triggerRatio float64) {
769769
goal = memstats.heap_marked + memstats.heap_marked*uint64(gcpercent)/100
770770
}
771771

772-
// If we let triggerRatio go too low, then if the application
773-
// is allocating very rapidly we might end up in a situation
774-
// where we're allocating black during a nearly always-on GC.
775-
// The result of this is a growing heap and ultimately an
776-
// increase in RSS. By capping us at a point >0, we're essentially
777-
// saying that we're OK using more CPU during the GC to prevent
778-
// this growth in RSS.
779-
//
780-
// The current constant was chosen empirically: given a sufficiently
781-
// fast/scalable allocator with 48 Ps that could drive the trigger ratio
782-
// to <0.05, this constant causes applications to retain the same peak
783-
// RSS compared to not having this allocator.
784-
const minTriggerRatio = 0.6
785-
786772
// Set the trigger ratio, capped to reasonable bounds.
787-
if triggerRatio < minTriggerRatio {
788-
// This can happen if the mutator is allocating very
789-
// quickly or the GC is scanning very slowly.
790-
triggerRatio = minTriggerRatio
791-
} else if gcpercent >= 0 {
773+
if gcpercent >= 0 {
774+
scalingFactor := float64(gcpercent) / 100
792775
// Ensure there's always a little margin so that the
793776
// mutator assist ratio isn't infinity.
794-
maxTriggerRatio := 0.95 * float64(gcpercent) / 100
777+
maxTriggerRatio := 0.95 * scalingFactor
795778
if triggerRatio > maxTriggerRatio {
796779
triggerRatio = maxTriggerRatio
797780
}
781+
782+
// If we let triggerRatio go too low, then if the application
783+
// is allocating very rapidly we might end up in a situation
784+
// where we're allocating black during a nearly always-on GC.
785+
// The result of this is a growing heap and ultimately an
786+
// increase in RSS. By capping us at a point >0, we're essentially
787+
// saying that we're OK using more CPU during the GC to prevent
788+
// this growth in RSS.
789+
//
790+
// The current constant was chosen empirically: given a sufficiently
791+
// fast/scalable allocator with 48 Ps that could drive the trigger ratio
792+
// to <0.05, this constant causes applications to retain the same peak
793+
// RSS compared to not having this allocator.
794+
minTriggerRatio := 0.6 * scalingFactor
795+
if triggerRatio < minTriggerRatio {
796+
triggerRatio = minTriggerRatio
797+
}
798+
} else if triggerRatio < 0 {
799+
// gcpercent < 0, so just make sure we're not getting a negative
800+
// triggerRatio. This case isn't expected to happen in practice,
801+
// and doesn't really matter because if gcpercent < 0 then we won't
802+
// ever consume triggerRatio further on in this function, but let's
803+
// just be defensive here; the triggerRatio being negative is almost
804+
// certainly undesirable.
805+
triggerRatio = 0
798806
}
799807
memstats.triggerRatio = triggerRatio
800808

0 commit comments

Comments
 (0)