diff --git a/src/runtime/mgc.go b/src/runtime/mgc.go index b0d46d0060..f65402e94c 100644 --- a/src/runtime/mgc.go +++ b/src/runtime/mgc.go @@ -153,22 +153,12 @@ func gcinit() { if unsafe.Sizeof(workbuf{}) != _WorkbufSize { throw("size of Workbuf is suboptimal") } - gcController.heapMinimum = defaultHeapMinimum - // No sweep on the first cycle. mheap_.sweepDrained = 1 - // Set a reasonable initial GC trigger. - gcController.triggerRatio = 7 / 8.0 - - // Fake a heapMarked value so it looks like a trigger at - // heapMinimum is the appropriate growth from heapMarked. - // This will go into computing the initial GC goal. - gcController.heapMarked = uint64(float64(gcController.heapMinimum) / (1 + gcController.triggerRatio)) - - // Set gcPercent from the environment. This will also compute - // and set the GC trigger and goal. - _ = setGCPercent(readGOGC()) + // Initialize GC pacer state. + // Use the environment variable GOGC for the initial gcPercent value. + gcController.init(readGOGC()) work.startSema = 1 work.markDoneSema = 1 diff --git a/src/runtime/mgcpacer.go b/src/runtime/mgcpacer.go index e594dbdc06..2366cdb84b 100644 --- a/src/runtime/mgcpacer.go +++ b/src/runtime/mgcpacer.go @@ -243,6 +243,21 @@ type gcControllerState struct { _ cpu.CacheLinePad } +func (c *gcControllerState) init(gcPercent int32) { + c.heapMinimum = defaultHeapMinimum + + // Set a reasonable initial GC trigger. + c.triggerRatio = 7 / 8.0 + + // Fake a heapMarked value so it looks like a trigger at + // heapMinimum is the appropriate growth from heapMarked. + // This will go into computing the initial GC goal. + c.heapMarked = uint64(float64(c.heapMinimum) / (1 + c.triggerRatio)) + + // This will also compute and set the GC trigger and goal. + _ = setGCPercent(gcPercent) +} + // startCycle resets the GC controller's state and computes estimates // for a new GC cycle. The caller must hold worldsema and the world // must be stopped.