From 1f39beb01a5ca6a9015d6a30af5a968a247f8d10 Mon Sep 17 00:00:00 2001 From: Austin Clements Date: Tue, 21 Apr 2015 13:46:54 -0400 Subject: [PATCH] runtime: avoid divide-by-zero in GC trigger controller The trigger controller computes GC CPU utilization by dividing by the wall-clock time that's passed since concurrent mark began. Since this delta is nanoseconds it's borderline impossible for it to be zero, but if it is zero we'll currently divide by zero. Be robust to this possibility by ignoring the utilization in the error term if no time has elapsed. Change-Id: I93dfc9e84735682af3e637f6538d1e7602634f09 Reviewed-on: https://go-review.googlesource.com/9175 Reviewed-by: Rick Hudson --- src/runtime/mgc.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/runtime/mgc.go b/src/runtime/mgc.go index 100fbf0b1c..0e4f7cb81b 100644 --- a/src/runtime/mgc.go +++ b/src/runtime/mgc.go @@ -424,7 +424,15 @@ func (c *gcControllerState) endCycle() { goalGrowthRatio := float64(gcpercent) / 100 actualGrowthRatio := float64(memstats.heap_live)/float64(memstats.heap_marked) - 1 duration := nanotime() - c.bgMarkStartTime - utilization := float64(c.assistTime+c.dedicatedMarkTime+c.fractionalMarkTime) / float64(duration*int64(gomaxprocs)) + var utilization float64 + if duration <= 0 { + // Avoid divide-by-zero computing utilization. This + // has the effect of ignoring the utilization in the + // error term. + utilization = gcGoalUtilization + } else { + utilization = float64(c.assistTime+c.dedicatedMarkTime+c.fractionalMarkTime) / float64(duration*int64(gomaxprocs)) + } triggerError := goalGrowthRatio - c.triggerRatio - utilization/gcGoalUtilization*(actualGrowthRatio-c.triggerRatio) // Finally, we adjust the trigger for next time by this error, -- 2.50.0