]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: pass nanotime and gomaxprocs into startCycle and endCycle explicitly
authorMichael Knyszek <mknyszek@google.com>
Fri, 1 Oct 2021 19:07:45 +0000 (15:07 -0400)
committerMichael Knyszek <mknyszek@google.com>
Fri, 29 Oct 2021 18:35:59 +0000 (18:35 +0000)
This is to facilitate testing of the pacer, since otherwise this is
accessing global state, which is impossible to stub out properly.

For #44167.

Change-Id: I52c3b51fc0ffff38e3bbe534bd66e5761c0003a8
Reviewed-on: https://go-review.googlesource.com/c/go/+/353353
Trust: Michael Knyszek <mknyszek@google.com>
Run-TryBot: Michael Knyszek <mknyszek@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Michael Pratt <mpratt@google.com>
src/runtime/mgc.go
src/runtime/mgcpacer.go

index cf53585dcde1efce97111a59d9e0b68bbcac5bb5..03711a9617da03649172888db8209b813dafc5bd 100644 (file)
@@ -663,7 +663,7 @@ func gcStart(trigger gcTrigger) {
 
        // Assists and workers can start the moment we start
        // the world.
-       gcController.startCycle(now)
+       gcController.startCycle(now, int(gomaxprocs))
        work.heapGoal = gcController.heapGoal
 
        // In STW mode, disable scheduling of user Gs. This may also
@@ -889,7 +889,7 @@ top:
        // endCycle depends on all gcWork cache stats being flushed.
        // The termination algorithm above ensured that up to
        // allocations since the ragged barrier.
-       nextTriggerRatio := gcController.endCycle(work.userForced)
+       nextTriggerRatio := gcController.endCycle(now, int(gomaxprocs), work.userForced)
 
        // Perform mark termination. This will restart the world.
        gcMarkTermination(nextTriggerRatio)
index ad7c4bb840dfe15ab76152afa1aaf3cb0bac76f6..160383db43712dfe1451b7a5b6ddcff8dff274c5 100644 (file)
@@ -290,7 +290,7 @@ func (c *gcControllerState) init(gcPercent int32) {
 // 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.
-func (c *gcControllerState) startCycle(markStartTime int64) {
+func (c *gcControllerState) startCycle(markStartTime int64, procs int) {
        c.scanWork = 0
        c.bgScanCredit = 0
        c.assistTime = 0
@@ -316,7 +316,7 @@ func (c *gcControllerState) startCycle(markStartTime int64) {
        // dedicated workers so that the utilization is closest to
        // 25%. For small GOMAXPROCS, this would introduce too much
        // error, so we add fractional workers in that case.
-       totalUtilizationGoal := float64(gomaxprocs) * gcBackgroundUtilization
+       totalUtilizationGoal := float64(procs) * gcBackgroundUtilization
        c.dedicatedMarkWorkersNeeded = int64(totalUtilizationGoal + 0.5)
        utilError := float64(c.dedicatedMarkWorkersNeeded)/totalUtilizationGoal - 1
        const maxUtilError = 0.3
@@ -329,14 +329,14 @@ func (c *gcControllerState) startCycle(markStartTime int64) {
                        // Too many dedicated workers.
                        c.dedicatedMarkWorkersNeeded--
                }
-               c.fractionalUtilizationGoal = (totalUtilizationGoal - float64(c.dedicatedMarkWorkersNeeded)) / float64(gomaxprocs)
+               c.fractionalUtilizationGoal = (totalUtilizationGoal - float64(c.dedicatedMarkWorkersNeeded)) / float64(procs)
        } else {
                c.fractionalUtilizationGoal = 0
        }
 
        // In STW mode, we just want dedicated workers.
        if debug.gcstoptheworld > 0 {
-               c.dedicatedMarkWorkersNeeded = int64(gomaxprocs)
+               c.dedicatedMarkWorkersNeeded = int64(procs)
                c.fractionalUtilizationGoal = 0
        }
 
@@ -464,7 +464,7 @@ func (c *gcControllerState) revise() {
 // endCycle computes the trigger ratio for the next cycle.
 // userForced indicates whether the current GC cycle was forced
 // by the application.
-func (c *gcControllerState) endCycle(userForced bool) float64 {
+func (c *gcControllerState) endCycle(now int64, procs int, userForced bool) float64 {
        // Record last heap goal for the scavenger.
        // We'll be updating the heap goal soon.
        gcController.lastHeapGoal = gcController.heapGoal
@@ -495,13 +495,13 @@ func (c *gcControllerState) endCycle(userForced bool) float64 {
        // heap growth is the error.
        goalGrowthRatio := c.effectiveGrowthRatio()
        actualGrowthRatio := float64(c.heapLive)/float64(c.heapMarked) - 1
-       assistDuration := nanotime() - c.markStartTime
+       assistDuration := now - c.markStartTime
 
        // Assume background mark hit its utilization goal.
        utilization := gcBackgroundUtilization
        // Add assist utilization; avoid divide by zero.
        if assistDuration > 0 {
-               utilization += float64(c.assistTime) / float64(assistDuration*int64(gomaxprocs))
+               utilization += float64(c.assistTime) / float64(assistDuration*int64(procs))
        }
 
        triggerError := goalGrowthRatio - c.triggerRatio - utilization/gcGoalUtilization*(actualGrowthRatio-c.triggerRatio)