Skip to content

Commit e7ab1a5

Browse files
committed
runtime: create setGCPercent method for gcControllerState
This change breaks out the computations done by setGCPercent into a method on gcControllerState for easier testing later. It leaves behind the global implementation details. For #44167. Change-Id: I3b0cf1475b032fcd4ebbd01cf4e80de0b55ce7b0 Reviewed-on: https://go-review.googlesource.com/c/go/+/306602 Trust: Michael Knyszek <[email protected]> Run-TryBot: Michael Knyszek <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Michael Pratt <[email protected]>
1 parent 9bce7b7 commit e7ab1a5

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

src/runtime/mgcpacer.go

+21-9
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ func (c *gcControllerState) init(gcPercent int32) {
255255
c.heapMarked = uint64(float64(c.heapMinimum) / (1 + c.triggerRatio))
256256

257257
// This will also compute and set the GC trigger and goal.
258-
_ = setGCPercent(gcPercent)
258+
c.setGCPercent(gcPercent)
259259
}
260260

261261
// startCycle resets the GC controller's state and computes estimates
@@ -784,19 +784,31 @@ func gcEffectiveGrowthRatio() float64 {
784784
return egogc
785785
}
786786

787+
// setGCPercent updates gcPercent and all related pacer state.
788+
// Returns the old value of gcPercent.
789+
//
790+
// The world must be stopped, or mheap_.lock must be held.
791+
func (c *gcControllerState) setGCPercent(in int32) int32 {
792+
assertWorldStoppedOrLockHeld(&mheap_.lock)
793+
794+
out := c.gcPercent
795+
if in < 0 {
796+
in = -1
797+
}
798+
c.gcPercent = in
799+
c.heapMinimum = defaultHeapMinimum * uint64(c.gcPercent) / 100
800+
// Update pacing in response to gcPercent change.
801+
c.commit(c.triggerRatio)
802+
803+
return out
804+
}
805+
787806
//go:linkname setGCPercent runtime/debug.setGCPercent
788807
func setGCPercent(in int32) (out int32) {
789808
// Run on the system stack since we grab the heap lock.
790809
systemstack(func() {
791810
lock(&mheap_.lock)
792-
out = gcController.gcPercent
793-
if in < 0 {
794-
in = -1
795-
}
796-
gcController.gcPercent = in
797-
gcController.heapMinimum = defaultHeapMinimum * uint64(gcController.gcPercent) / 100
798-
// Update pacing in response to gcPercent change.
799-
gcController.commit(gcController.triggerRatio)
811+
out = gcController.setGCPercent(in)
800812
unlock(&mheap_.lock)
801813
})
802814

0 commit comments

Comments
 (0)