Skip to content

Commit

Permalink
runtime: pass nanotime and gomaxprocs into startCycle and endCycle ex…
Browse files Browse the repository at this point in the history
…plicitly

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>
  • Loading branch information
mknyszek committed Oct 29, 2021
1 parent 9da6415 commit 5ec2427
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/runtime/mgc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
14 changes: 7 additions & 7 deletions src/runtime/mgcpacer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 5ec2427

Please sign in to comment.