aboutsummaryrefslogtreecommitdiff
path: root/src/runtime/mgcscavenge.go
diff options
context:
space:
mode:
authorMichael Anthony Knyszek <mknyszek@google.com>2019-09-18 15:03:50 +0000
committerMichael Knyszek <mknyszek@google.com>2019-11-08 16:21:04 +0000
commitae4534e6598fe905d3ebefae44afef07e853b1d0 (patch)
tree3052e5f9bb07210295a69ea8eebfe6be4cb141b8 /src/runtime/mgcscavenge.go
parent814c5058bbbd70e706b0305d823e83aa0112d5a4 (diff)
downloadgo-ae4534e6598fe905d3ebefae44afef07e853b1d0.tar.gz
go-ae4534e6598fe905d3ebefae44afef07e853b1d0.zip
runtime: ensure heap memstats are updated atomically
For the most part, heap memstats are already updated atomically when passed down to OS-level memory functions (e.g. sysMap). Elsewhere, however, they're updated with the heap lock. In order to facilitate holding the heap lock for less time during allocation paths, this change more consistently makes the update of these statistics atomic by calling mSysStat{Inc,Dec} appropriately instead of simply adding or subtracting. It also ensures these values are loaded atomically. Furthermore, an undocumented but safe update condition for these memstats is during STW, at which point using atomics is unnecessary. This change also documents this condition in mstats.go. Updates #35112. Change-Id: I87d0b6c27b98c88099acd2563ea23f8da1239b66 Reviewed-on: https://go-review.googlesource.com/c/go/+/196638 Run-TryBot: Michael Knyszek <mknyszek@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Austin Clements <austin@google.com>
Diffstat (limited to 'src/runtime/mgcscavenge.go')
-rw-r--r--src/runtime/mgcscavenge.go7
1 files changed, 3 insertions, 4 deletions
diff --git a/src/runtime/mgcscavenge.go b/src/runtime/mgcscavenge.go
index 8592ccaa77..d79a43fb1c 100644
--- a/src/runtime/mgcscavenge.go
+++ b/src/runtime/mgcscavenge.go
@@ -57,6 +57,7 @@ package runtime
import (
"math/bits"
+ "runtime/internal/atomic"
"unsafe"
)
@@ -78,10 +79,8 @@ const (
)
// heapRetained returns an estimate of the current heap RSS.
-//
-// mheap_.lock must be held or the world must be stopped.
func heapRetained() uint64 {
- return memstats.heap_sys - memstats.heap_released
+ return atomic.Load64(&memstats.heap_sys) - atomic.Load64(&memstats.heap_released)
}
// gcPaceScavenger updates the scavenger's pacing, particularly
@@ -489,7 +488,7 @@ func (s *pageAlloc) scavengeRangeLocked(ci chunkIdx, base, npages uint) {
// Update global accounting only when not in test, otherwise
// the runtime's accounting will be wrong.
- memstats.heap_released += uint64(npages) * pageSize
+ mSysStatInc(&memstats.heap_released, uintptr(npages)*pageSize)
}
// fillAligned returns x but with all zeroes in m-aligned