Skip to content

Commit ccca9c9

Browse files
rhyshaclements
authored andcommitted
runtime: reduce GC assist extra credit
Mutator goroutines that allocate memory during the concurrent mark phase are required to spend some time assisting the garbage collector. The magnitude of this mandatory assistance is proportional to the goroutine's allocation debt and subject to the assistance ratio as calculated by the pacer. When assisting the garbage collector, a mutator goroutine will go beyond paying off its allocation debt. It will build up extra credit to amortize the overhead of the assist. In fast-allocating applications with high assist ratios, building up this credit can take the affected goroutine's entire time slice. Reduce the penalty on each goroutine being selected to assist the GC in two ways, to spread the responsibility more evenly. First, do a consistent amount of extra scan work without regard for the pacer's assistance ratio. Second, reduce the magnitude of the extra scan work so it can be completed within a few hundred microseconds. Commentary on gcOverAssistWork is by Austin Clements, originally in https://golang.org/cl/24704 Updates #14812 Fixes #16432 Change-Id: I436f899e778c20daa314f3e9f0e2a1bbd53b43e1 Reviewed-on: https://go-review.googlesource.com/25155 Run-TryBot: Austin Clements <austin@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Austin Clements <austin@google.com> Reviewed-by: Rick Hudson <rlh@golang.org> Reviewed-by: Chris Broadfoot <cbro@golang.org>
1 parent c80e0d3 commit ccca9c9

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

src/runtime/mgc.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -741,11 +741,10 @@ const gcCreditSlack = 2000
741741
// can accumulate on a P before updating gcController.assistTime.
742742
const gcAssistTimeSlack = 5000
743743

744-
// gcOverAssistBytes determines how many extra allocation bytes of
745-
// assist credit a GC assist builds up when an assist happens. This
746-
// amortizes the cost of an assist by pre-paying for this many bytes
747-
// of future allocations.
748-
const gcOverAssistBytes = 1 << 20
744+
// gcOverAssistWork determines how many extra units of scan work a GC
745+
// assist does when an assist happens. This amortizes the cost of an
746+
// assist by pre-paying for this many bytes of future allocations.
747+
const gcOverAssistWork = 64 << 10
749748

750749
var work struct {
751750
full uint64 // lock-free list of full blocks workbuf

src/runtime/mgcmark.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -393,10 +393,15 @@ func gcAssistAlloc(gp *g) {
393393
}
394394

395395
// Compute the amount of scan work we need to do to make the
396-
// balance positive. We over-assist to build up credit for
397-
// future allocations and amortize the cost of assisting.
398-
debtBytes := -gp.gcAssistBytes + gcOverAssistBytes
396+
// balance positive. When the required amount of work is low,
397+
// we over-assist to build up credit for future allocations
398+
// and amortize the cost of assisting.
399+
debtBytes := -gp.gcAssistBytes
399400
scanWork := int64(gcController.assistWorkPerByte * float64(debtBytes))
401+
if scanWork < gcOverAssistWork {
402+
scanWork = gcOverAssistWork
403+
debtBytes = int64(gcController.assistBytesPerWork * float64(scanWork))
404+
}
400405

401406
retry:
402407
// Steal as much credit as we can from the background GC's

0 commit comments

Comments
 (0)