Skip to content

Commit 8e112a7

Browse files
committed
runtime: move heapLive and heapScan updates into a method
This change moves heapLive and heapScan updates on gcController into a method for better testability. It's also less error-prone because code that updates these fields needs to remember to emit traces and/or call gcController.revise; this method now handles those cases. For #44167. Change-Id: I3d6f2e7abb22def27c93feacff50162b0b074da2 Reviewed-on: https://go-review.googlesource.com/c/go/+/309275 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 4a5d78f commit 8e112a7

File tree

2 files changed

+27
-29
lines changed

2 files changed

+27
-29
lines changed

src/runtime/mcache.go

+10-29
Original file line numberDiff line numberDiff line change
@@ -184,24 +184,13 @@ func (c *mcache) refill(spc spanClass) {
184184
}
185185
memstats.heapStats.release()
186186

187-
// Update gcController.heapLive with the same assumption.
188-
usedBytes := uintptr(s.allocCount) * s.elemsize
189-
atomic.Xadd64(&gcController.heapLive, int64(s.npages*pageSize)-int64(usedBytes))
190-
187+
// Update heapLive with the same assumption.
191188
// While we're here, flush scanAlloc, since we have to call
192189
// revise anyway.
193-
atomic.Xadd64(&gcController.heapScan, int64(c.scanAlloc))
190+
usedBytes := uintptr(s.allocCount) * s.elemsize
191+
gcController.update(int64(s.npages*pageSize)-int64(usedBytes), int64(c.scanAlloc))
194192
c.scanAlloc = 0
195193

196-
if trace.enabled {
197-
// gcController.heapLive changed.
198-
traceHeapAlloc()
199-
}
200-
if gcBlackenEnabled != 0 {
201-
// gcController.heapLive and heapScan changed.
202-
gcController.revise()
203-
}
204-
205194
c.alloc[spc] = s
206195
}
207196

@@ -230,15 +219,8 @@ func (c *mcache) allocLarge(size uintptr, noscan bool) *mspan {
230219
atomic.Xadduintptr(&stats.largeAllocCount, 1)
231220
memstats.heapStats.release()
232221

233-
// Update gcController.heapLive and revise pacing if needed.
234-
atomic.Xadd64(&gcController.heapLive, int64(npages*pageSize))
235-
if trace.enabled {
236-
// Trace that a heap alloc occurred because gcController.heapLive changed.
237-
traceHeapAlloc()
238-
}
239-
if gcBlackenEnabled != 0 {
240-
gcController.revise()
241-
}
222+
// Update heapLive.
223+
gcController.update(int64(s.npages*pageSize), 0)
242224

243225
// Put the large span in the mcentral swept list so that it's
244226
// visible to the background sweeper.
@@ -250,10 +232,11 @@ func (c *mcache) allocLarge(size uintptr, noscan bool) *mspan {
250232

251233
func (c *mcache) releaseAll() {
252234
// Take this opportunity to flush scanAlloc.
253-
atomic.Xadd64(&gcController.heapScan, int64(c.scanAlloc))
235+
scanAlloc := int64(c.scanAlloc)
254236
c.scanAlloc = 0
255237

256238
sg := mheap_.sweepgen
239+
dHeapLive := int64(0)
257240
for i := range c.alloc {
258241
s := c.alloc[i]
259242
if s != &emptymspan {
@@ -270,7 +253,7 @@ func (c *mcache) releaseAll() {
270253
// gcController.heapLive was totally recomputed since
271254
// caching this span, so we don't do this for
272255
// stale spans.
273-
atomic.Xadd64(&gcController.heapLive, -int64(n)*int64(s.elemsize))
256+
dHeapLive -= int64(n) * int64(s.elemsize)
274257
}
275258
// Release the span to the mcentral.
276259
mheap_.central[i].mcentral.uncacheSpan(s)
@@ -287,10 +270,8 @@ func (c *mcache) releaseAll() {
287270
c.tinyAllocs = 0
288271
memstats.heapStats.release()
289272

290-
// Updated heapScan and possible gcController.heapLive.
291-
if gcBlackenEnabled != 0 {
292-
gcController.revise()
293-
}
273+
// Updated heapScan and heapLive.
274+
gcController.update(dHeapLive, scanAlloc)
294275
}
295276

296277
// prepareForSweep flushes c if the system has entered a new sweep phase

src/runtime/mgcpacer.go

+17
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,23 @@ func (c *gcControllerState) logWorkTime(mode gcMarkWorkerMode, duration int64) {
669669
}
670670
}
671671

672+
func (c *gcControllerState) update(dHeapLive, dHeapScan int64) {
673+
if dHeapLive != 0 {
674+
atomic.Xadd64(&gcController.heapLive, dHeapLive)
675+
if trace.enabled {
676+
// gcController.heapLive changed.
677+
traceHeapAlloc()
678+
}
679+
}
680+
if dHeapScan != 0 {
681+
atomic.Xadd64(&gcController.heapScan, dHeapScan)
682+
}
683+
if gcBlackenEnabled != 0 {
684+
// gcController.heapLive and heapScan changed.
685+
c.revise()
686+
}
687+
}
688+
672689
// commit sets the trigger ratio and updates everything
673690
// derived from it: the absolute trigger, the heap goal, mark pacing,
674691
// and sweep pacing.

0 commit comments

Comments
 (0)