Skip to content

Commit a3e90dc

Browse files
committed
runtime: add eager scavenging details to GODEBUG=scavtrace=1
Also, clean up atomics on released-per-cycle while we're here. For #57069. Change-Id: I14026e8281f01dea1e8c8de6aa8944712b7b24d9 Reviewed-on: https://go-review.googlesource.com/c/go/+/495916 Reviewed-by: Michael Pratt <[email protected]> Run-TryBot: Michael Knyszek <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent 0bbb54a commit a3e90dc

File tree

5 files changed

+34
-17
lines changed

5 files changed

+34
-17
lines changed

src/runtime/extern.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,13 @@ It is a comma-separated list of name=val pairs setting these named variables:
158158
scavenger as well as the total amount of memory returned to the operating system
159159
and an estimate of physical memory utilization. The format of this line is subject
160160
to change, but currently it is:
161-
scav # KiB work, # KiB total, #% util
161+
scav # KiB work (bg), # KiB work (eager), # KiB total, #% util
162162
where the fields are as follows:
163-
# KiB work the amount of memory returned to the OS since the last line
164-
# KiB total the total amount of memory returned to the OS
165-
#% util the fraction of all unscavenged memory which is in-use
163+
# KiB work (bg) the amount of memory returned to the OS in the background since
164+
the last line
165+
# KiB work (eager) the amount of memory returned to the OS eagerly since the last line
166+
# KiB now the amount of address space currently returned to the OS
167+
#% util the fraction of all unscavenged heap memory which is in-use
166168
If the line ends with "(forced)", then scavenging was forced by a
167169
debug.FreeOSMemory() call.
168170

src/runtime/mgcscavenge.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,7 @@ func bgscavenge(c chan int) {
658658
scavenger.park()
659659
continue
660660
}
661-
atomic.Xadduintptr(&mheap_.pages.scav.released, released)
661+
mheap_.pages.scav.releasedBg.Add(released)
662662
scavenger.sleep(workTime)
663663
}
664664
}
@@ -696,13 +696,14 @@ func (p *pageAlloc) scavenge(nbytes uintptr, shouldStop func() bool, force bool)
696696
// application.
697697
//
698698
// scavenger.lock must be held.
699-
func printScavTrace(released uintptr, forced bool) {
699+
func printScavTrace(releasedBg, releasedEager uintptr, forced bool) {
700700
assertLockHeld(&scavenger.lock)
701701

702702
printlock()
703703
print("scav ",
704-
released>>10, " KiB work, ",
705-
gcController.heapReleased.load()>>10, " KiB total, ",
704+
releasedBg>>10, " KiB work (bg), ",
705+
releasedEager>>10, " KiB work (eager), ",
706+
gcController.heapReleased.load()>>10, " KiB now, ",
706707
(gcController.heapInUse.load()*100)/heapRetained(), "% util",
707708
)
708709
if forced {

src/runtime/mgcsweep.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -425,9 +425,17 @@ func sweepone() uintptr {
425425
if debug.scavtrace > 0 {
426426
systemstack(func() {
427427
lock(&mheap_.lock)
428-
released := atomic.Loaduintptr(&mheap_.pages.scav.released)
429-
printScavTrace(released, false)
430-
atomic.Storeuintptr(&mheap_.pages.scav.released, 0)
428+
429+
// Get released stats.
430+
releasedBg := mheap_.pages.scav.releasedBg.Load()
431+
releasedEager := mheap_.pages.scav.releasedEager.Load()
432+
433+
// Print the line.
434+
printScavTrace(releasedBg, releasedEager, false)
435+
436+
// Update the stats.
437+
mheap_.pages.scav.releasedBg.Add(-releasedBg)
438+
mheap_.pages.scav.releasedEager.Add(-releasedEager)
431439
unlock(&mheap_.lock)
432440
})
433441
}

src/runtime/mheap.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1323,10 +1323,12 @@ HaveSpan:
13231323
track := pp.limiterEvent.start(limiterEventScavengeAssist, start)
13241324

13251325
// Scavenge, but back out if the limiter turns on.
1326-
h.pages.scavenge(bytesToScavenge, func() bool {
1326+
released := h.pages.scavenge(bytesToScavenge, func() bool {
13271327
return gcCPULimiter.limiting()
13281328
}, forceScavenge)
13291329

1330+
mheap_.pages.scav.releasedEager.Add(released)
1331+
13301332
// Finish up accounting.
13311333
now = nanotime()
13321334
if track {
@@ -1658,7 +1660,7 @@ func (h *mheap) scavengeAll() {
16581660
gp.m.mallocing--
16591661

16601662
if debug.scavtrace > 0 {
1661-
printScavTrace(released, true)
1663+
printScavTrace(0, released, true)
16621664
}
16631665
}
16641666

src/runtime/mpagealloc.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
package runtime
4949

5050
import (
51+
"runtime/internal/atomic"
5152
"unsafe"
5253
)
5354

@@ -270,10 +271,13 @@ type pageAlloc struct {
270271
// scavenge.
271272
index scavengeIndex
272273

273-
// released is the amount of memory released this scavenge cycle.
274-
//
275-
// Updated atomically.
276-
released uintptr
274+
// releasedBg is the amount of memory released in the background this
275+
// scavenge cycle.
276+
releasedBg atomic.Uintptr
277+
278+
// releasedEager is the amount of memory released eagerly this scavenge
279+
// cycle.
280+
releasedEager atomic.Uintptr
277281
}
278282

279283
// mheap_.lock. This level of indirection makes it possible

0 commit comments

Comments
 (0)