diff options
| author | Austin Clements <austin@google.com> | 2017-01-03 10:15:55 -0700 |
|---|---|---|
| committer | Austin Clements <austin@google.com> | 2017-03-04 02:56:37 +0000 |
| commit | 4a7cf960c38d72e9f0c6f00e46e013be2a35d56e (patch) | |
| tree | 1e47293cf8e12313d8ea713ed7b35ee8b157a787 /src/runtime/mstats.go | |
| parent | 3399fd254dcdf4e8a9be8c327076de5f9efe1b3a (diff) | |
| download | go-4a7cf960c38d72e9f0c6f00e46e013be2a35d56e.tar.xz | |
runtime: make ReadMemStats STW for < 25µs
Currently ReadMemStats stops the world for ~1.7 ms/GB of heap because
it collects statistics from every single span. For large heaps, this
can be quite costly. This is particularly unfortunate because many
production infrastructures call this function regularly to collect and
report statistics.
Fix this by tracking the necessary cumulative statistics in the
mcaches. ReadMemStats still has to stop the world to stabilize these
statistics, but there are only O(GOMAXPROCS) mcaches to collect
statistics from, so this pause is only 25µs even at GOMAXPROCS=100.
Fixes #13613.
Change-Id: I3c0a4e14833f4760dab675efc1916e73b4c0032a
Reviewed-on: https://go-review.googlesource.com/34937
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rick Hudson <rlh@golang.org>
Diffstat (limited to 'src/runtime/mstats.go')
| -rw-r--r-- | src/runtime/mstats.go | 44 |
1 files changed, 22 insertions, 22 deletions
diff --git a/src/runtime/mstats.go b/src/runtime/mstats.go index b34bbf9a67..36b5b5077d 100644 --- a/src/runtime/mstats.go +++ b/src/runtime/mstats.go @@ -534,37 +534,37 @@ func updatememstats() { // Aggregate local stats. cachestats() - // Scan all spans and count number of alive objects. - lock(&mheap_.lock) - for _, s := range mheap_.allspans { - if s.state != mSpanInUse { + // Collect allocation stats. This is safe and consistent + // because the world is stopped. + var smallFree, totalAlloc, totalFree uint64 + for i := range mheap_.central { + if i == 0 { + memstats.nmalloc += mheap_.nlargealloc + totalAlloc += mheap_.largealloc + totalFree += mheap_.largefree + memstats.nfree += mheap_.nlargefree continue } - if s.sizeclass == 0 { - memstats.nmalloc++ - memstats.alloc += uint64(s.elemsize) - } else { - memstats.nmalloc += uint64(s.allocCount) - memstats.by_size[s.sizeclass].nmalloc += uint64(s.allocCount) - memstats.alloc += uint64(s.allocCount) * uint64(s.elemsize) - } - } - unlock(&mheap_.lock) + // The mcaches are now empty, so mcentral stats are + // up-to-date. + c := &mheap_.central[i].mcentral + memstats.nmalloc += c.nmalloc + memstats.by_size[i].nmalloc += c.nmalloc + totalAlloc += c.nmalloc * uint64(class_to_size[i]) - // Aggregate by size class. - smallfree := uint64(0) - memstats.nfree = mheap_.nlargefree - for i := 0; i < len(memstats.by_size); i++ { + // The mcache stats have been flushed to mheap_. memstats.nfree += mheap_.nsmallfree[i] memstats.by_size[i].nfree = mheap_.nsmallfree[i] - memstats.by_size[i].nmalloc += mheap_.nsmallfree[i] - smallfree += mheap_.nsmallfree[i] * uint64(class_to_size[i]) + smallFree += mheap_.nsmallfree[i] * uint64(class_to_size[i]) } + totalFree += smallFree + memstats.nfree += memstats.tinyallocs - memstats.nmalloc += memstats.nfree + memstats.nmalloc += memstats.tinyallocs // Calculate derived stats. - memstats.total_alloc = memstats.alloc + mheap_.largefree + smallfree + memstats.total_alloc = totalAlloc + memstats.alloc = totalAlloc - totalFree memstats.heap_alloc = memstats.alloc memstats.heap_objects = memstats.nmalloc - memstats.nfree } |
