Skip to content

Commit 5ec2427

Browse files
committed
runtime: pass nanotime and gomaxprocs into startCycle and endCycle explicitly
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 <[email protected]> Run-TryBot: Michael Knyszek <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Michael Pratt <[email protected]>
1 parent 9da6415 commit 5ec2427

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

src/runtime/mgc.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,7 @@ func gcStart(trigger gcTrigger) {
663663

664664
// Assists and workers can start the moment we start
665665
// the world.
666-
gcController.startCycle(now)
666+
gcController.startCycle(now, int(gomaxprocs))
667667
work.heapGoal = gcController.heapGoal
668668

669669
// In STW mode, disable scheduling of user Gs. This may also
@@ -889,7 +889,7 @@ top:
889889
// endCycle depends on all gcWork cache stats being flushed.
890890
// The termination algorithm above ensured that up to
891891
// allocations since the ragged barrier.
892-
nextTriggerRatio := gcController.endCycle(work.userForced)
892+
nextTriggerRatio := gcController.endCycle(now, int(gomaxprocs), work.userForced)
893893

894894
// Perform mark termination. This will restart the world.
895895
gcMarkTermination(nextTriggerRatio)

src/runtime/mgcpacer.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ func (c *gcControllerState) init(gcPercent int32) {
290290
// startCycle resets the GC controller's state and computes estimates
291291
// for a new GC cycle. The caller must hold worldsema and the world
292292
// must be stopped.
293-
func (c *gcControllerState) startCycle(markStartTime int64) {
293+
func (c *gcControllerState) startCycle(markStartTime int64, procs int) {
294294
c.scanWork = 0
295295
c.bgScanCredit = 0
296296
c.assistTime = 0
@@ -316,7 +316,7 @@ func (c *gcControllerState) startCycle(markStartTime int64) {
316316
// dedicated workers so that the utilization is closest to
317317
// 25%. For small GOMAXPROCS, this would introduce too much
318318
// error, so we add fractional workers in that case.
319-
totalUtilizationGoal := float64(gomaxprocs) * gcBackgroundUtilization
319+
totalUtilizationGoal := float64(procs) * gcBackgroundUtilization
320320
c.dedicatedMarkWorkersNeeded = int64(totalUtilizationGoal + 0.5)
321321
utilError := float64(c.dedicatedMarkWorkersNeeded)/totalUtilizationGoal - 1
322322
const maxUtilError = 0.3
@@ -329,14 +329,14 @@ func (c *gcControllerState) startCycle(markStartTime int64) {
329329
// Too many dedicated workers.
330330
c.dedicatedMarkWorkersNeeded--
331331
}
332-
c.fractionalUtilizationGoal = (totalUtilizationGoal - float64(c.dedicatedMarkWorkersNeeded)) / float64(gomaxprocs)
332+
c.fractionalUtilizationGoal = (totalUtilizationGoal - float64(c.dedicatedMarkWorkersNeeded)) / float64(procs)
333333
} else {
334334
c.fractionalUtilizationGoal = 0
335335
}
336336

337337
// In STW mode, we just want dedicated workers.
338338
if debug.gcstoptheworld > 0 {
339-
c.dedicatedMarkWorkersNeeded = int64(gomaxprocs)
339+
c.dedicatedMarkWorkersNeeded = int64(procs)
340340
c.fractionalUtilizationGoal = 0
341341
}
342342

@@ -464,7 +464,7 @@ func (c *gcControllerState) revise() {
464464
// endCycle computes the trigger ratio for the next cycle.
465465
// userForced indicates whether the current GC cycle was forced
466466
// by the application.
467-
func (c *gcControllerState) endCycle(userForced bool) float64 {
467+
func (c *gcControllerState) endCycle(now int64, procs int, userForced bool) float64 {
468468
// Record last heap goal for the scavenger.
469469
// We'll be updating the heap goal soon.
470470
gcController.lastHeapGoal = gcController.heapGoal
@@ -495,13 +495,13 @@ func (c *gcControllerState) endCycle(userForced bool) float64 {
495495
// heap growth is the error.
496496
goalGrowthRatio := c.effectiveGrowthRatio()
497497
actualGrowthRatio := float64(c.heapLive)/float64(c.heapMarked) - 1
498-
assistDuration := nanotime() - c.markStartTime
498+
assistDuration := now - c.markStartTime
499499

500500
// Assume background mark hit its utilization goal.
501501
utilization := gcBackgroundUtilization
502502
// Add assist utilization; avoid divide by zero.
503503
if assistDuration > 0 {
504-
utilization += float64(c.assistTime) / float64(assistDuration*int64(gomaxprocs))
504+
utilization += float64(c.assistTime) / float64(assistDuration*int64(procs))
505505
}
506506

507507
triggerError := goalGrowthRatio - c.triggerRatio - utilization/gcGoalUtilization*(actualGrowthRatio-c.triggerRatio)

0 commit comments

Comments
 (0)