From: Michael Anthony Knyszek Date: Sat, 13 Aug 2022 16:37:09 +0000 (+0000) Subject: runtime: factor out GC assist credit accounting X-Git-Tag: go1.20rc1~664 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=987f94fa038d4a66fa174ddc4267d8718a029581;p=gostls13.git runtime: factor out GC assist credit accounting No-op change in preparation for arenas. For #51317. Change-Id: I0777f21763fcd34957b7e709580cf2b7a962ba67 Reviewed-on: https://go-review.googlesource.com/c/go/+/423365 Reviewed-by: Cherry Mui Run-TryBot: Michael Knyszek TryBot-Result: Gopher Robot --- diff --git a/src/runtime/malloc.go b/src/runtime/malloc.go index f2b93c04f4..c18ed07d49 100644 --- a/src/runtime/malloc.go +++ b/src/runtime/malloc.go @@ -895,24 +895,7 @@ func mallocgc(size uintptr, typ *_type, needzero bool) unsafe.Pointer { // assistG is the G to charge for this allocation, or nil if // GC is not currently active. - var assistG *g - if gcBlackenEnabled != 0 { - // Charge the current user G for this allocation. - assistG = getg() - if assistG.m.curg != nil { - assistG = assistG.m.curg - } - // Charge the allocation against the G. We'll account - // for internal fragmentation at the end of mallocgc. - assistG.gcAssistBytes -= int64(size) - - if assistG.gcAssistBytes < 0 { - // This G is in debt. Assist the GC to correct - // this before allocating. This must happen - // before disabling preemption. - gcAssistAlloc(assistG) - } - } + assistG := deductAssistCredit(size) // Set mp.mallocing to keep from being preempted by GC. mp := acquirem() @@ -1165,6 +1148,34 @@ func mallocgc(size uintptr, typ *_type, needzero bool) unsafe.Pointer { return x } +// deductAssistCredit reduces the current G's assist credit +// by size bytes, and assists the GC if necessary. +// +// Caller must be preemptible. +// +// Returns the G for which the assist credit was accounted. +func deductAssistCredit(size uintptr) *g { + var assistG *g + if gcBlackenEnabled != 0 { + // Charge the current user G for this allocation. + assistG = getg() + if assistG.m.curg != nil { + assistG = assistG.m.curg + } + // Charge the allocation against the G. We'll account + // for internal fragmentation at the end of mallocgc. + assistG.gcAssistBytes -= int64(size) + + if assistG.gcAssistBytes < 0 { + // This G is in debt. Assist the GC to correct + // this before allocating. This must happen + // before disabling preemption. + gcAssistAlloc(assistG) + } + } + return assistG +} + // memclrNoHeapPointersChunked repeatedly calls memclrNoHeapPointers // on chunks of the buffer to be zeroed, with opportunities for preemption // along the way. memclrNoHeapPointers contains no safepoints and also