2014-11-11 17:05:02 -05:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
|
|
// Garbage collector (GC).
|
|
|
|
|
//
|
2014-12-09 13:25:45 -05:00
|
|
|
// The GC runs concurrently with mutator threads, is type accurate (aka precise), allows multiple
|
|
|
|
|
// GC thread to run in parallel. It is a concurrent mark and sweep that uses a write barrier. It is
|
2014-11-15 08:00:38 -05:00
|
|
|
// non-generational and non-compacting. Allocation is done using size segregated per P allocation
|
|
|
|
|
// areas to minimize fragmentation while eliminating locks in the common case.
|
2014-11-11 17:05:02 -05:00
|
|
|
//
|
2014-11-15 08:00:38 -05:00
|
|
|
// The algorithm decomposes into several steps.
|
|
|
|
|
// This is a high level description of the algorithm being used. For an overview of GC a good
|
|
|
|
|
// place to start is Richard Jones' gchandbook.org.
|
|
|
|
|
//
|
|
|
|
|
// The algorithm's intellectual heritage includes Dijkstra's on-the-fly algorithm, see
|
|
|
|
|
// Edsger W. Dijkstra, Leslie Lamport, A. J. Martin, C. S. Scholten, and E. F. M. Steffens. 1978.
|
2014-12-09 10:15:18 -05:00
|
|
|
// On-the-fly garbage collection: an exercise in cooperation. Commun. ACM 21, 11 (November 1978),
|
2014-12-09 13:25:45 -05:00
|
|
|
// 966-975.
|
2014-11-15 08:00:38 -05:00
|
|
|
// For journal quality proofs that these steps are complete, correct, and terminate see
|
|
|
|
|
// Hudson, R., and Moss, J.E.B. Copying Garbage Collection without stopping the world.
|
|
|
|
|
// Concurrency and Computation: Practice and Experience 15(3-5), 2003.
|
2014-11-11 17:05:02 -05:00
|
|
|
//
|
2016-12-22 17:30:23 -07:00
|
|
|
// 1. GC performs sweep termination.
|
|
|
|
|
//
|
|
|
|
|
// a. Stop the world. This causes all Ps to reach a GC safe-point.
|
|
|
|
|
//
|
|
|
|
|
// b. Sweep any unswept spans. There will only be unswept spans if
|
|
|
|
|
// this GC cycle was forced before the expected time.
|
|
|
|
|
//
|
|
|
|
|
// 2. GC performs the "mark 1" sub-phase. In this sub-phase, Ps are
|
|
|
|
|
// allowed to locally cache parts of the work queue.
|
|
|
|
|
//
|
|
|
|
|
// a. Prepare for the mark phase by setting gcphase to _GCmark
|
|
|
|
|
// (from _GCoff), enabling the write barrier, enabling mutator
|
|
|
|
|
// assists, and enqueueing root mark jobs. No objects may be
|
|
|
|
|
// scanned until all Ps have enabled the write barrier, which is
|
|
|
|
|
// accomplished using STW.
|
|
|
|
|
//
|
|
|
|
|
// b. Start the world. From this point, GC work is done by mark
|
|
|
|
|
// workers started by the scheduler and by assists performed as
|
|
|
|
|
// part of allocation. The write barrier shades both the
|
|
|
|
|
// overwritten pointer and the new pointer value for any pointer
|
|
|
|
|
// writes (see mbarrier.go for details). Newly allocated objects
|
|
|
|
|
// are immediately marked black.
|
|
|
|
|
//
|
|
|
|
|
// c. GC performs root marking jobs. This includes scanning all
|
|
|
|
|
// stacks, shading all globals, and shading any heap pointers in
|
|
|
|
|
// off-heap runtime data structures. Scanning a stack stops a
|
|
|
|
|
// goroutine, shades any pointers found on its stack, and then
|
|
|
|
|
// resumes the goroutine.
|
|
|
|
|
//
|
|
|
|
|
// d. GC drains the work queue of grey objects, scanning each grey
|
|
|
|
|
// object to black and shading all pointers found in the object
|
|
|
|
|
// (which in turn may add those pointers to the work queue).
|
|
|
|
|
//
|
|
|
|
|
// 3. Once the global work queue is empty (but local work queue caches
|
|
|
|
|
// may still contain work), GC performs the "mark 2" sub-phase.
|
|
|
|
|
//
|
|
|
|
|
// a. GC stops all workers, disables local work queue caches,
|
|
|
|
|
// flushes each P's local work queue cache to the global work queue
|
|
|
|
|
// cache, and reenables workers.
|
|
|
|
|
//
|
|
|
|
|
// b. GC again drains the work queue, as in 2d above.
|
|
|
|
|
//
|
|
|
|
|
// 4. Once the work queue is empty, GC performs mark termination.
|
|
|
|
|
//
|
|
|
|
|
// a. Stop the world.
|
|
|
|
|
//
|
|
|
|
|
// b. Set gcphase to _GCmarktermination, and disable workers and
|
|
|
|
|
// assists.
|
|
|
|
|
//
|
|
|
|
|
// c. Drain any remaining work from the work queue (typically there
|
|
|
|
|
// will be none).
|
|
|
|
|
//
|
|
|
|
|
// d. Perform other housekeeping like flushing mcaches.
|
|
|
|
|
//
|
|
|
|
|
// 5. GC performs the sweep phase.
|
|
|
|
|
//
|
|
|
|
|
// a. Prepare for the sweep phase by setting gcphase to _GCoff,
|
|
|
|
|
// setting up sweep state and disabling the write barrier.
|
|
|
|
|
//
|
|
|
|
|
// b. Start the world. From this point on, newly allocated objects
|
|
|
|
|
// are white, and allocating sweeps spans before use if necessary.
|
|
|
|
|
//
|
|
|
|
|
// c. GC does concurrent sweeping in the background and in response
|
|
|
|
|
// to allocation. See description below.
|
|
|
|
|
//
|
|
|
|
|
// 6. When sufficient allocation has taken place, replay the sequence
|
|
|
|
|
// starting with 1 above. See discussion of GC rate below.
|
2014-11-15 08:00:38 -05:00
|
|
|
|
2014-11-11 17:05:02 -05:00
|
|
|
// Concurrent sweep.
|
runtime: introduce heap_live; replace use of heap_alloc in GC
Currently there are two main consumers of memstats.heap_alloc:
updatememstats (aka ReadMemStats) and shouldtriggergc.
updatememstats recomputes heap_alloc from the ground up, so we don't
need to keep heap_alloc up to date for it. shouldtriggergc wants to
know how many bytes were marked by the previous GC plus how many bytes
have been allocated since then, but this *isn't* what heap_alloc
tracks. heap_alloc also includes objects that are not marked and
haven't yet been swept.
Introduce a new memstat called heap_live that actually tracks what
shouldtriggergc wants to know and stop keeping heap_alloc up to date.
Unlike heap_alloc, heap_live follows a simple sawtooth that drops
during each mark termination and increases monotonically between GCs.
heap_alloc, on the other hand, has much more complicated behavior: it
may drop during sweep termination, slowly decreases from background
sweeping between GCs, is roughly unaffected by allocation as long as
there are unswept spans (because we sweep and allocate at the same
rate), and may go up after background sweeping is done depending on
the GC trigger.
heap_live simplifies computing next_gc and using it to figure out when
to trigger garbage collection. Currently, we guess next_gc at the end
of a cycle and update it as we sweep and get a better idea of how much
heap was marked. Now, since we're directly tracking how much heap is
marked, we can directly compute next_gc.
This also corrects bugs that could cause us to trigger GC early.
Currently, in any case where sweep termination actually finds spans to
sweep, heap_alloc is an overestimation of live heap, so we'll trigger
GC too early. heap_live, on the other hand, is unaffected by sweeping.
Change-Id: I1f96807b6ed60d4156e8173a8e68745ffc742388
Reviewed-on: https://go-review.googlesource.com/8389
Reviewed-by: Russ Cox <rsc@golang.org>
2015-03-30 18:01:32 -04:00
|
|
|
//
|
2014-11-11 17:05:02 -05:00
|
|
|
// The sweep phase proceeds concurrently with normal program execution.
|
|
|
|
|
// The heap is swept span-by-span both lazily (when a goroutine needs another span)
|
|
|
|
|
// and concurrently in a background goroutine (this helps programs that are not CPU bound).
|
runtime: introduce heap_live; replace use of heap_alloc in GC
Currently there are two main consumers of memstats.heap_alloc:
updatememstats (aka ReadMemStats) and shouldtriggergc.
updatememstats recomputes heap_alloc from the ground up, so we don't
need to keep heap_alloc up to date for it. shouldtriggergc wants to
know how many bytes were marked by the previous GC plus how many bytes
have been allocated since then, but this *isn't* what heap_alloc
tracks. heap_alloc also includes objects that are not marked and
haven't yet been swept.
Introduce a new memstat called heap_live that actually tracks what
shouldtriggergc wants to know and stop keeping heap_alloc up to date.
Unlike heap_alloc, heap_live follows a simple sawtooth that drops
during each mark termination and increases monotonically between GCs.
heap_alloc, on the other hand, has much more complicated behavior: it
may drop during sweep termination, slowly decreases from background
sweeping between GCs, is roughly unaffected by allocation as long as
there are unswept spans (because we sweep and allocate at the same
rate), and may go up after background sweeping is done depending on
the GC trigger.
heap_live simplifies computing next_gc and using it to figure out when
to trigger garbage collection. Currently, we guess next_gc at the end
of a cycle and update it as we sweep and get a better idea of how much
heap was marked. Now, since we're directly tracking how much heap is
marked, we can directly compute next_gc.
This also corrects bugs that could cause us to trigger GC early.
Currently, in any case where sweep termination actually finds spans to
sweep, heap_alloc is an overestimation of live heap, so we'll trigger
GC too early. heap_live, on the other hand, is unaffected by sweeping.
Change-Id: I1f96807b6ed60d4156e8173a8e68745ffc742388
Reviewed-on: https://go-review.googlesource.com/8389
Reviewed-by: Russ Cox <rsc@golang.org>
2015-03-30 18:01:32 -04:00
|
|
|
// At the end of STW mark termination all spans are marked as "needs sweeping".
|
|
|
|
|
//
|
|
|
|
|
// The background sweeper goroutine simply sweeps spans one-by-one.
|
|
|
|
|
//
|
|
|
|
|
// To avoid requesting more OS memory while there are unswept spans, when a
|
|
|
|
|
// goroutine needs another span, it first attempts to reclaim that much memory
|
|
|
|
|
// by sweeping. When a goroutine needs to allocate a new small-object span, it
|
|
|
|
|
// sweeps small-object spans for the same object size until it frees at least
|
|
|
|
|
// one object. When a goroutine needs to allocate large-object span from heap,
|
|
|
|
|
// it sweeps spans until it frees at least that many pages into heap. There is
|
|
|
|
|
// one case where this may not suffice: if a goroutine sweeps and frees two
|
|
|
|
|
// nonadjacent one-page spans to the heap, it will allocate a new two-page
|
|
|
|
|
// span, but there can still be other one-page unswept spans which could be
|
|
|
|
|
// combined into a two-page span.
|
|
|
|
|
//
|
2014-11-11 17:05:02 -05:00
|
|
|
// It's critical to ensure that no operations proceed on unswept spans (that would corrupt
|
|
|
|
|
// mark bits in GC bitmap). During GC all mcaches are flushed into the central cache,
|
|
|
|
|
// so they are empty. When a goroutine grabs a new span into mcache, it sweeps it.
|
|
|
|
|
// When a goroutine explicitly frees an object or sets a finalizer, it ensures that
|
|
|
|
|
// the span is swept (either by sweeping it, or by waiting for the concurrent sweep to finish).
|
|
|
|
|
// The finalizer goroutine is kicked off only when all spans are swept.
|
|
|
|
|
// When the next GC starts, it sweeps all not-yet-swept spans (if any).
|
|
|
|
|
|
2014-11-15 08:00:38 -05:00
|
|
|
// GC rate.
|
|
|
|
|
// Next GC is after we've allocated an extra amount of memory proportional to
|
|
|
|
|
// the amount already in use. The proportion is controlled by GOGC environment variable
|
|
|
|
|
// (100 by default). If GOGC=100 and we're using 4M, we'll GC again when we get to 8M
|
|
|
|
|
// (this mark is tracked in next_gc variable). This keeps the GC cost in linear
|
|
|
|
|
// proportion to the allocation cost. Adjusting GOGC just changes the linear constant
|
|
|
|
|
// (and also the amount of extra memory used).
|
|
|
|
|
|
runtime: bound scanobject to ~100 µs
Currently the time spent in scanobject is proportional to the size of
the object being scanned. Since scanobject is non-preemptible, large
objects can cause significant goroutine (and even whole application)
delays through several means:
1. If a GC assist picks up a large object, the allocating goroutine is
blocked for the whole scan, even if that scan well exceeds that
goroutine's debt.
2. Since the scheduler does not run on the P performing a large object
scan, goroutines in that P's run queue do not run unless they are
stolen by another P (which can take some time). If there are a few
large objects, all of the Ps may get tied up so the scheduler
doesn't run anywhere.
3. Even if a large object is scanned by a background worker and other
Ps are still running the scheduler, the large object scan doesn't
flush background credit until the whole scan is done. This can
easily cause all allocations to block in assists, waiting for
credit, causing an effective STW.
Fix this by splitting large objects into 128 KB "oblets" and scanning
at most one oblet at a time. Since we can scan 1–2 MB/ms, this equates
to bounding scanobject at roughly 100 µs. This improves assist
behavior both because assists can no longer get "unlucky" and be stuck
scanning a large object, and because it causes the background worker
to flush credit and unblock assists more frequently when scanning
large objects. This also improves GC parallelism if the heap consists
primarily of a small number of very large objects by letting multiple
workers scan a large objects in parallel.
Fixes #10345. Fixes #16293.
This substantially improves goroutine latency in the benchmark from
issue #16293, which exercises several forms of very large objects:
name old max-latency new max-latency delta
SliceNoPointer-12 154µs ± 1% 155µs ± 2% ~ (p=0.087 n=13+12)
SlicePointer-12 314ms ± 1% 5.94ms ±138% -98.11% (p=0.000 n=19+20)
SliceLivePointer-12 1148ms ± 0% 4.72ms ±167% -99.59% (p=0.000 n=19+20)
MapNoPointer-12 72509µs ± 1% 408µs ±325% -99.44% (p=0.000 n=19+18)
ChanPointer-12 313ms ± 0% 4.74ms ±140% -98.49% (p=0.000 n=18+20)
ChanLivePointer-12 1147ms ± 0% 3.30ms ±149% -99.71% (p=0.000 n=19+20)
name old P99.9-latency new P99.9-latency delta
SliceNoPointer-12 113µs ±25% 107µs ±12% ~ (p=0.153 n=20+18)
SlicePointer-12 309450µs ± 0% 133µs ±23% -99.96% (p=0.000 n=20+20)
SliceLivePointer-12 961ms ± 0% 1.35ms ±27% -99.86% (p=0.000 n=20+20)
MapNoPointer-12 448µs ±288% 119µs ±18% -73.34% (p=0.000 n=18+20)
ChanPointer-12 309450µs ± 0% 134µs ±23% -99.96% (p=0.000 n=20+19)
ChanLivePointer-12 961ms ± 0% 1.35ms ±27% -99.86% (p=0.000 n=20+20)
This has negligible effect on all metrics from the garbage, JSON, and
HTTP x/benchmarks.
It shows slight improvement on some of the go1 benchmarks,
particularly Revcomp, which uses some multi-megabyte buffers:
name old time/op new time/op delta
BinaryTree17-12 2.46s ± 1% 2.47s ± 1% +0.32% (p=0.012 n=20+20)
Fannkuch11-12 2.82s ± 0% 2.81s ± 0% -0.61% (p=0.000 n=17+20)
FmtFprintfEmpty-12 50.8ns ± 5% 50.5ns ± 2% ~ (p=0.197 n=17+19)
FmtFprintfString-12 131ns ± 1% 132ns ± 0% +0.57% (p=0.000 n=20+16)
FmtFprintfInt-12 117ns ± 0% 116ns ± 0% -0.47% (p=0.000 n=15+20)
FmtFprintfIntInt-12 180ns ± 0% 179ns ± 1% -0.78% (p=0.000 n=16+20)
FmtFprintfPrefixedInt-12 186ns ± 1% 185ns ± 1% -0.55% (p=0.000 n=19+20)
FmtFprintfFloat-12 263ns ± 1% 271ns ± 0% +2.84% (p=0.000 n=18+20)
FmtManyArgs-12 741ns ± 1% 742ns ± 1% ~ (p=0.190 n=19+19)
GobDecode-12 7.44ms ± 0% 7.35ms ± 1% -1.21% (p=0.000 n=20+20)
GobEncode-12 6.22ms ± 1% 6.21ms ± 1% ~ (p=0.336 n=20+19)
Gzip-12 220ms ± 1% 219ms ± 1% ~ (p=0.130 n=19+19)
Gunzip-12 37.9ms ± 0% 37.9ms ± 1% ~ (p=1.000 n=20+19)
HTTPClientServer-12 82.5µs ± 3% 82.6µs ± 3% ~ (p=0.776 n=20+19)
JSONEncode-12 16.4ms ± 1% 16.5ms ± 2% +0.49% (p=0.003 n=18+19)
JSONDecode-12 53.7ms ± 1% 54.1ms ± 1% +0.71% (p=0.000 n=19+18)
Mandelbrot200-12 4.19ms ± 1% 4.20ms ± 1% ~ (p=0.452 n=19+19)
GoParse-12 3.38ms ± 1% 3.37ms ± 1% ~ (p=0.123 n=19+19)
RegexpMatchEasy0_32-12 72.1ns ± 1% 71.8ns ± 1% ~ (p=0.397 n=19+17)
RegexpMatchEasy0_1K-12 242ns ± 0% 242ns ± 0% ~ (p=0.168 n=17+20)
RegexpMatchEasy1_32-12 72.1ns ± 1% 72.1ns ± 1% ~ (p=0.538 n=18+19)
RegexpMatchEasy1_1K-12 385ns ± 1% 384ns ± 1% ~ (p=0.388 n=20+20)
RegexpMatchMedium_32-12 112ns ± 1% 112ns ± 3% ~ (p=0.539 n=20+20)
RegexpMatchMedium_1K-12 34.4µs ± 2% 34.4µs ± 2% ~ (p=0.628 n=18+18)
RegexpMatchHard_32-12 1.80µs ± 1% 1.80µs ± 1% ~ (p=0.522 n=18+19)
RegexpMatchHard_1K-12 54.0µs ± 1% 54.1µs ± 1% ~ (p=0.647 n=20+19)
Revcomp-12 387ms ± 1% 369ms ± 5% -4.89% (p=0.000 n=17+19)
Template-12 62.3ms ± 1% 62.0ms ± 0% -0.48% (p=0.002 n=20+17)
TimeParse-12 314ns ± 1% 314ns ± 0% ~ (p=1.011 n=20+13)
TimeFormat-12 358ns ± 0% 354ns ± 0% -1.12% (p=0.000 n=17+20)
[Geo mean] 53.5µs 53.3µs -0.23%
Change-Id: I2a0a179d1d6bf7875dd054b7693dd12d2a340132
Reviewed-on: https://go-review.googlesource.com/23540
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rick Hudson <rlh@golang.org>
2016-05-27 21:04:40 -04:00
|
|
|
// Oblets
|
|
|
|
|
//
|
|
|
|
|
// In order to prevent long pauses while scanning large objects and to
|
|
|
|
|
// improve parallelism, the garbage collector breaks up scan jobs for
|
|
|
|
|
// objects larger than maxObletBytes into "oblets" of at most
|
|
|
|
|
// maxObletBytes. When scanning encounters the beginning of a large
|
|
|
|
|
// object, it scans only the first oblet and enqueues the remaining
|
|
|
|
|
// oblets as new scan jobs.
|
|
|
|
|
|
2014-11-11 17:05:02 -05:00
|
|
|
package runtime
|
|
|
|
|
|
2015-11-02 14:09:24 -05:00
|
|
|
import (
|
|
|
|
|
"runtime/internal/atomic"
|
2015-11-11 12:39:30 -05:00
|
|
|
"runtime/internal/sys"
|
2015-11-02 14:09:24 -05:00
|
|
|
"unsafe"
|
|
|
|
|
)
|
2014-11-11 17:05:02 -05:00
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
_DebugGC = 0
|
|
|
|
|
_ConcurrentSweep = true
|
|
|
|
|
_FinBlockSize = 4 * 1024
|
2015-09-14 14:28:09 -04:00
|
|
|
|
2015-08-03 09:25:23 -04:00
|
|
|
// sweepMinHeapDistance is a lower bound on the heap distance
|
|
|
|
|
// (in bytes) reserved for concurrent sweeping between GC
|
|
|
|
|
// cycles. This will be scaled by gcpercent/100.
|
|
|
|
|
sweepMinHeapDistance = 1024 * 1024
|
2014-11-11 17:05:02 -05:00
|
|
|
)
|
|
|
|
|
|
2015-05-16 21:14:37 -04:00
|
|
|
// heapminimum is the minimum heap size at which to trigger GC.
|
|
|
|
|
// For small heaps, this overrides the usual GOGC*live set rule.
|
|
|
|
|
//
|
|
|
|
|
// When there is a very small live set but a lot of allocation, simply
|
|
|
|
|
// collecting when the heap reaches GOGC*live results in many GC
|
|
|
|
|
// cycles and high total per-GC overhead. This minimum amortizes this
|
|
|
|
|
// per-GC overhead while keeping the heap reasonably small.
|
|
|
|
|
//
|
|
|
|
|
// During initialization this is set to 4MB*GOGC/100. In the case of
|
|
|
|
|
// GOGC==0, this will set heapminimum to 0, resulting in constant
|
|
|
|
|
// collection even when the heap size is small, which is useful for
|
|
|
|
|
// debugging.
|
|
|
|
|
var heapminimum uint64 = defaultHeapMinimum
|
|
|
|
|
|
|
|
|
|
// defaultHeapMinimum is the value of heapminimum for GOGC==100.
|
|
|
|
|
const defaultHeapMinimum = 4 << 20
|
2014-11-15 08:00:38 -05:00
|
|
|
|
2015-02-19 13:38:46 -05:00
|
|
|
// Initialized from $GOGC. GOGC=off means no GC.
|
|
|
|
|
var gcpercent int32
|
2015-01-06 14:58:49 -05:00
|
|
|
|
2015-02-19 13:38:46 -05:00
|
|
|
func gcinit() {
|
|
|
|
|
if unsafe.Sizeof(workbuf{}) != _WorkbufSize {
|
|
|
|
|
throw("size of Workbuf is suboptimal")
|
2014-11-11 17:05:02 -05:00
|
|
|
}
|
runtime: switch to gcWork abstraction
This converts the garbage collector from directly manipulating work
buffers to using the new gcWork abstraction.
The previous management of work buffers was rather ad hoc. As a
result, switching to the gcWork abstraction changes many details of
work buffer management.
If greyobject fills a work buffer, it can now pull from work.partial
in addition to work.empty.
Previously, gcDrain started with a partial or empty work buffer and
fetched an empty work buffer if it filled its current buffer (in
greyobject). Now, gcDrain starts with a full work buffer and fetches
an partial or empty work buffer if it fills its current buffer (in
greyobject). The original behavior was bad because gcDrain would
immediately drop the empty work buffer returned by greyobject and
fetch a full work buffer, which greyobject was likely to immediately
overflow, fetching another empty work buffer, etc. The new behavior
isn't great at the start because greyobject is likely to immediately
overflow the full buffer, but the steady-state behavior should be more
stable. Both before and after this change, gcDrain fetches a full
work buffer if it drains its current buffer. Basically all of these
choices are bad; the right answer is to use a dual work buffer scheme.
Previously, shade always fetched a work buffer (though usually from
m.currentwbuf), even if the object was already marked. Now it only
fetches a work buffer if it actually greys an object.
Change-Id: I8b880ed660eb63135236fa5d5678f0c1c041881f
Reviewed-on: https://go-review.googlesource.com/5232
Reviewed-by: Russ Cox <rsc@golang.org>
Reviewed-by: Rick Hudson <rlh@golang.org>
2015-02-17 10:53:31 -05:00
|
|
|
|
2017-04-04 13:26:28 -04:00
|
|
|
// No sweep on the first cycle.
|
|
|
|
|
mheap_.sweepdone = 1
|
2017-03-31 17:09:41 -04:00
|
|
|
|
|
|
|
|
// Set a reasonable initial GC trigger.
|
|
|
|
|
memstats.triggerRatio = 7 / 8.0
|
2017-04-04 13:26:28 -04:00
|
|
|
|
|
|
|
|
// Fake a heap_marked value so it looks like a trigger at
|
|
|
|
|
// heapminimum is the appropriate growth from heap_marked.
|
|
|
|
|
// This will go into computing the initial GC goal.
|
|
|
|
|
memstats.heap_marked = uint64(float64(heapminimum) / (1 + memstats.triggerRatio))
|
|
|
|
|
|
|
|
|
|
// Set gcpercent from the environment. This will also compute
|
|
|
|
|
// and set the GC trigger and goal.
|
|
|
|
|
_ = setGCPercent(readgogc())
|
|
|
|
|
|
2015-10-23 14:15:18 -04:00
|
|
|
work.startSema = 1
|
2015-10-26 11:27:37 -04:00
|
|
|
work.markDoneSema = 1
|
2014-11-11 17:05:02 -05:00
|
|
|
}
|
|
|
|
|
|
2015-05-06 15:58:20 -04:00
|
|
|
func readgogc() int32 {
|
|
|
|
|
p := gogetenv("GOGC")
|
|
|
|
|
if p == "off" {
|
|
|
|
|
return -1
|
|
|
|
|
}
|
2016-10-30 01:54:19 +02:00
|
|
|
if n, ok := atoi32(p); ok {
|
|
|
|
|
return n
|
|
|
|
|
}
|
|
|
|
|
return 100
|
2015-05-06 15:58:20 -04:00
|
|
|
}
|
|
|
|
|
|
2015-03-05 16:04:17 -05:00
|
|
|
// gcenable is called after the bulk of the runtime initialization,
|
|
|
|
|
// just before we're about to start letting user code run.
|
|
|
|
|
// It kicks off the background sweeper goroutine and enables GC.
|
|
|
|
|
func gcenable() {
|
|
|
|
|
c := make(chan int, 1)
|
|
|
|
|
go bgsweep(c)
|
|
|
|
|
<-c
|
|
|
|
|
memstats.enablegc = true // now that runtime is initialized, GC is okay
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-16 20:19:14 +13:00
|
|
|
//go:linkname setGCPercent runtime/debug.setGCPercent
|
2015-02-19 13:38:46 -05:00
|
|
|
func setGCPercent(in int32) (out int32) {
|
|
|
|
|
lock(&mheap_.lock)
|
|
|
|
|
out = gcpercent
|
|
|
|
|
if in < 0 {
|
|
|
|
|
in = -1
|
2014-11-11 17:05:02 -05:00
|
|
|
}
|
2015-02-19 13:38:46 -05:00
|
|
|
gcpercent = in
|
2015-05-16 21:14:37 -04:00
|
|
|
heapminimum = defaultHeapMinimum * uint64(gcpercent) / 100
|
2017-04-04 13:26:28 -04:00
|
|
|
// Update pacing in response to gcpercent change.
|
|
|
|
|
gcSetTriggerRatio(memstats.triggerRatio)
|
2015-02-19 13:38:46 -05:00
|
|
|
unlock(&mheap_.lock)
|
|
|
|
|
return out
|
2014-11-11 17:05:02 -05:00
|
|
|
}
|
|
|
|
|
|
runtime: replace needwb() with writeBarrierEnabled
Reduce the write barrier check to a single load and compare
so that it can be inlined into write barrier use sites.
Makes the standard write barrier a little faster too.
name old new delta
BenchmarkBinaryTree17 17.9s × (0.99,1.01) 17.9s × (1.00,1.01) ~
BenchmarkFannkuch11 4.35s × (1.00,1.00) 4.43s × (1.00,1.00) +1.81%
BenchmarkFmtFprintfEmpty 120ns × (0.93,1.06) 110ns × (1.00,1.06) -7.92%
BenchmarkFmtFprintfString 479ns × (0.99,1.00) 487ns × (0.99,1.00) +1.67%
BenchmarkFmtFprintfInt 452ns × (0.99,1.02) 450ns × (0.99,1.00) ~
BenchmarkFmtFprintfIntInt 766ns × (0.99,1.01) 762ns × (1.00,1.00) ~
BenchmarkFmtFprintfPrefixedInt 576ns × (0.98,1.01) 584ns × (0.99,1.01) ~
BenchmarkFmtFprintfFloat 730ns × (1.00,1.01) 738ns × (1.00,1.00) +1.16%
BenchmarkFmtManyArgs 2.84µs × (0.99,1.00) 2.80µs × (1.00,1.01) -1.22%
BenchmarkGobDecode 39.3ms × (0.98,1.01) 39.0ms × (0.99,1.00) ~
BenchmarkGobEncode 39.5ms × (0.99,1.01) 37.8ms × (0.98,1.01) -4.33%
BenchmarkGzip 663ms × (1.00,1.01) 661ms × (0.99,1.01) ~
BenchmarkGunzip 143ms × (1.00,1.00) 142ms × (1.00,1.00) ~
BenchmarkHTTPClientServer 132µs × (0.99,1.01) 132µs × (0.99,1.01) ~
BenchmarkJSONEncode 57.4ms × (0.99,1.01) 56.3ms × (0.99,1.01) -1.96%
BenchmarkJSONDecode 139ms × (0.99,1.00) 138ms × (0.99,1.01) ~
BenchmarkMandelbrot200 6.03ms × (1.00,1.00) 6.01ms × (1.00,1.00) ~
BenchmarkGoParse 10.3ms × (0.89,1.14) 10.2ms × (0.87,1.05) ~
BenchmarkRegexpMatchEasy0_32 209ns × (1.00,1.00) 208ns × (1.00,1.00) ~
BenchmarkRegexpMatchEasy0_1K 591ns × (0.99,1.00) 588ns × (1.00,1.00) ~
BenchmarkRegexpMatchEasy1_32 184ns × (0.99,1.02) 182ns × (0.99,1.01) ~
BenchmarkRegexpMatchEasy1_1K 1.01µs × (1.00,1.00) 0.99µs × (1.00,1.01) -2.33%
BenchmarkRegexpMatchMedium_32 330ns × (1.00,1.00) 323ns × (1.00,1.01) -2.12%
BenchmarkRegexpMatchMedium_1K 92.6µs × (1.00,1.00) 89.9µs × (1.00,1.00) -2.92%
BenchmarkRegexpMatchHard_32 4.80µs × (0.95,1.00) 4.72µs × (0.95,1.01) ~
BenchmarkRegexpMatchHard_1K 136µs × (1.00,1.00) 133µs × (1.00,1.01) -1.86%
BenchmarkRevcomp 900ms × (0.99,1.04) 900ms × (1.00,1.05) ~
BenchmarkTemplate 172ms × (1.00,1.00) 168ms × (0.99,1.01) -2.07%
BenchmarkTimeParse 637ns × (1.00,1.00) 637ns × (1.00,1.00) ~
BenchmarkTimeFormat 744ns × (1.00,1.01) 738ns × (1.00,1.00) -0.67%
Change-Id: I4ecc925805da1f5ee264377f1f7574f54ee575e7
Reviewed-on: https://go-review.googlesource.com/9321
Reviewed-by: Austin Clements <austin@google.com>
2015-04-24 14:00:55 -04:00
|
|
|
// Garbage collector phase.
|
2016-07-25 15:53:15 +03:00
|
|
|
// Indicates to write barrier and synchronization task to perform.
|
runtime: replace needwb() with writeBarrierEnabled
Reduce the write barrier check to a single load and compare
so that it can be inlined into write barrier use sites.
Makes the standard write barrier a little faster too.
name old new delta
BenchmarkBinaryTree17 17.9s × (0.99,1.01) 17.9s × (1.00,1.01) ~
BenchmarkFannkuch11 4.35s × (1.00,1.00) 4.43s × (1.00,1.00) +1.81%
BenchmarkFmtFprintfEmpty 120ns × (0.93,1.06) 110ns × (1.00,1.06) -7.92%
BenchmarkFmtFprintfString 479ns × (0.99,1.00) 487ns × (0.99,1.00) +1.67%
BenchmarkFmtFprintfInt 452ns × (0.99,1.02) 450ns × (0.99,1.00) ~
BenchmarkFmtFprintfIntInt 766ns × (0.99,1.01) 762ns × (1.00,1.00) ~
BenchmarkFmtFprintfPrefixedInt 576ns × (0.98,1.01) 584ns × (0.99,1.01) ~
BenchmarkFmtFprintfFloat 730ns × (1.00,1.01) 738ns × (1.00,1.00) +1.16%
BenchmarkFmtManyArgs 2.84µs × (0.99,1.00) 2.80µs × (1.00,1.01) -1.22%
BenchmarkGobDecode 39.3ms × (0.98,1.01) 39.0ms × (0.99,1.00) ~
BenchmarkGobEncode 39.5ms × (0.99,1.01) 37.8ms × (0.98,1.01) -4.33%
BenchmarkGzip 663ms × (1.00,1.01) 661ms × (0.99,1.01) ~
BenchmarkGunzip 143ms × (1.00,1.00) 142ms × (1.00,1.00) ~
BenchmarkHTTPClientServer 132µs × (0.99,1.01) 132µs × (0.99,1.01) ~
BenchmarkJSONEncode 57.4ms × (0.99,1.01) 56.3ms × (0.99,1.01) -1.96%
BenchmarkJSONDecode 139ms × (0.99,1.00) 138ms × (0.99,1.01) ~
BenchmarkMandelbrot200 6.03ms × (1.00,1.00) 6.01ms × (1.00,1.00) ~
BenchmarkGoParse 10.3ms × (0.89,1.14) 10.2ms × (0.87,1.05) ~
BenchmarkRegexpMatchEasy0_32 209ns × (1.00,1.00) 208ns × (1.00,1.00) ~
BenchmarkRegexpMatchEasy0_1K 591ns × (0.99,1.00) 588ns × (1.00,1.00) ~
BenchmarkRegexpMatchEasy1_32 184ns × (0.99,1.02) 182ns × (0.99,1.01) ~
BenchmarkRegexpMatchEasy1_1K 1.01µs × (1.00,1.00) 0.99µs × (1.00,1.01) -2.33%
BenchmarkRegexpMatchMedium_32 330ns × (1.00,1.00) 323ns × (1.00,1.01) -2.12%
BenchmarkRegexpMatchMedium_1K 92.6µs × (1.00,1.00) 89.9µs × (1.00,1.00) -2.92%
BenchmarkRegexpMatchHard_32 4.80µs × (0.95,1.00) 4.72µs × (0.95,1.01) ~
BenchmarkRegexpMatchHard_1K 136µs × (1.00,1.00) 133µs × (1.00,1.01) -1.86%
BenchmarkRevcomp 900ms × (0.99,1.04) 900ms × (1.00,1.05) ~
BenchmarkTemplate 172ms × (1.00,1.00) 168ms × (0.99,1.01) -2.07%
BenchmarkTimeParse 637ns × (1.00,1.00) 637ns × (1.00,1.00) ~
BenchmarkTimeFormat 744ns × (1.00,1.01) 738ns × (1.00,1.00) -0.67%
Change-Id: I4ecc925805da1f5ee264377f1f7574f54ee575e7
Reviewed-on: https://go-review.googlesource.com/9321
Reviewed-by: Austin Clements <austin@google.com>
2015-04-24 14:00:55 -04:00
|
|
|
var gcphase uint32
|
2015-11-13 17:45:22 -08:00
|
|
|
|
|
|
|
|
// The compiler knows about this variable.
|
2017-04-07 18:06:12 -04:00
|
|
|
// If you change it, you must change builtin/runtime.go, too.
|
|
|
|
|
// If you change the first four bytes, you must also change the write
|
|
|
|
|
// barrier insertion code.
|
2015-11-13 17:45:22 -08:00
|
|
|
var writeBarrier struct {
|
2016-05-06 10:12:57 -07:00
|
|
|
enabled bool // compiler emits a check of this before calling write barrier
|
|
|
|
|
pad [3]byte // compiler uses 32-bit load for "enabled" field
|
|
|
|
|
needed bool // whether we need a write barrier for current GC phase
|
|
|
|
|
cgo bool // whether we need a write barrier for a cgo check
|
|
|
|
|
alignme uint64 // guarantee alignment so that compiler can use a 32 or 64-bit load
|
2015-11-13 17:45:22 -08:00
|
|
|
}
|
runtime: replace needwb() with writeBarrierEnabled
Reduce the write barrier check to a single load and compare
so that it can be inlined into write barrier use sites.
Makes the standard write barrier a little faster too.
name old new delta
BenchmarkBinaryTree17 17.9s × (0.99,1.01) 17.9s × (1.00,1.01) ~
BenchmarkFannkuch11 4.35s × (1.00,1.00) 4.43s × (1.00,1.00) +1.81%
BenchmarkFmtFprintfEmpty 120ns × (0.93,1.06) 110ns × (1.00,1.06) -7.92%
BenchmarkFmtFprintfString 479ns × (0.99,1.00) 487ns × (0.99,1.00) +1.67%
BenchmarkFmtFprintfInt 452ns × (0.99,1.02) 450ns × (0.99,1.00) ~
BenchmarkFmtFprintfIntInt 766ns × (0.99,1.01) 762ns × (1.00,1.00) ~
BenchmarkFmtFprintfPrefixedInt 576ns × (0.98,1.01) 584ns × (0.99,1.01) ~
BenchmarkFmtFprintfFloat 730ns × (1.00,1.01) 738ns × (1.00,1.00) +1.16%
BenchmarkFmtManyArgs 2.84µs × (0.99,1.00) 2.80µs × (1.00,1.01) -1.22%
BenchmarkGobDecode 39.3ms × (0.98,1.01) 39.0ms × (0.99,1.00) ~
BenchmarkGobEncode 39.5ms × (0.99,1.01) 37.8ms × (0.98,1.01) -4.33%
BenchmarkGzip 663ms × (1.00,1.01) 661ms × (0.99,1.01) ~
BenchmarkGunzip 143ms × (1.00,1.00) 142ms × (1.00,1.00) ~
BenchmarkHTTPClientServer 132µs × (0.99,1.01) 132µs × (0.99,1.01) ~
BenchmarkJSONEncode 57.4ms × (0.99,1.01) 56.3ms × (0.99,1.01) -1.96%
BenchmarkJSONDecode 139ms × (0.99,1.00) 138ms × (0.99,1.01) ~
BenchmarkMandelbrot200 6.03ms × (1.00,1.00) 6.01ms × (1.00,1.00) ~
BenchmarkGoParse 10.3ms × (0.89,1.14) 10.2ms × (0.87,1.05) ~
BenchmarkRegexpMatchEasy0_32 209ns × (1.00,1.00) 208ns × (1.00,1.00) ~
BenchmarkRegexpMatchEasy0_1K 591ns × (0.99,1.00) 588ns × (1.00,1.00) ~
BenchmarkRegexpMatchEasy1_32 184ns × (0.99,1.02) 182ns × (0.99,1.01) ~
BenchmarkRegexpMatchEasy1_1K 1.01µs × (1.00,1.00) 0.99µs × (1.00,1.01) -2.33%
BenchmarkRegexpMatchMedium_32 330ns × (1.00,1.00) 323ns × (1.00,1.01) -2.12%
BenchmarkRegexpMatchMedium_1K 92.6µs × (1.00,1.00) 89.9µs × (1.00,1.00) -2.92%
BenchmarkRegexpMatchHard_32 4.80µs × (0.95,1.00) 4.72µs × (0.95,1.01) ~
BenchmarkRegexpMatchHard_1K 136µs × (1.00,1.00) 133µs × (1.00,1.01) -1.86%
BenchmarkRevcomp 900ms × (0.99,1.04) 900ms × (1.00,1.05) ~
BenchmarkTemplate 172ms × (1.00,1.00) 168ms × (0.99,1.01) -2.07%
BenchmarkTimeParse 637ns × (1.00,1.00) 637ns × (1.00,1.00) ~
BenchmarkTimeFormat 744ns × (1.00,1.01) 738ns × (1.00,1.00) -0.67%
Change-Id: I4ecc925805da1f5ee264377f1f7574f54ee575e7
Reviewed-on: https://go-review.googlesource.com/9321
Reviewed-by: Austin Clements <austin@google.com>
2015-04-24 14:00:55 -04:00
|
|
|
|
|
|
|
|
// gcBlackenEnabled is 1 if mutator assists and background mark
|
|
|
|
|
// workers are allowed to blacken objects. This must only be set when
|
|
|
|
|
// gcphase == _GCmark.
|
|
|
|
|
var gcBlackenEnabled uint32
|
|
|
|
|
|
2015-06-01 18:16:03 -04:00
|
|
|
// gcBlackenPromptly indicates that optimizations that may
|
|
|
|
|
// hide work from the global work queue should be disabled.
|
|
|
|
|
//
|
|
|
|
|
// If gcBlackenPromptly is true, per-P gcWork caches should
|
|
|
|
|
// be flushed immediately and new objects should be allocated black.
|
|
|
|
|
//
|
|
|
|
|
// There is a tension between allocating objects white and
|
|
|
|
|
// allocating them black. If white and the objects die before being
|
|
|
|
|
// marked they can be collected during this GC cycle. On the other
|
|
|
|
|
// hand allocating them black will reduce _GCmarktermination latency
|
|
|
|
|
// since more work is done in the mark phase. This tension is resolved
|
|
|
|
|
// by allocating white until the mark phase is approaching its end and
|
|
|
|
|
// then allocating black for the remainder of the mark phase.
|
|
|
|
|
var gcBlackenPromptly bool
|
|
|
|
|
|
runtime: replace needwb() with writeBarrierEnabled
Reduce the write barrier check to a single load and compare
so that it can be inlined into write barrier use sites.
Makes the standard write barrier a little faster too.
name old new delta
BenchmarkBinaryTree17 17.9s × (0.99,1.01) 17.9s × (1.00,1.01) ~
BenchmarkFannkuch11 4.35s × (1.00,1.00) 4.43s × (1.00,1.00) +1.81%
BenchmarkFmtFprintfEmpty 120ns × (0.93,1.06) 110ns × (1.00,1.06) -7.92%
BenchmarkFmtFprintfString 479ns × (0.99,1.00) 487ns × (0.99,1.00) +1.67%
BenchmarkFmtFprintfInt 452ns × (0.99,1.02) 450ns × (0.99,1.00) ~
BenchmarkFmtFprintfIntInt 766ns × (0.99,1.01) 762ns × (1.00,1.00) ~
BenchmarkFmtFprintfPrefixedInt 576ns × (0.98,1.01) 584ns × (0.99,1.01) ~
BenchmarkFmtFprintfFloat 730ns × (1.00,1.01) 738ns × (1.00,1.00) +1.16%
BenchmarkFmtManyArgs 2.84µs × (0.99,1.00) 2.80µs × (1.00,1.01) -1.22%
BenchmarkGobDecode 39.3ms × (0.98,1.01) 39.0ms × (0.99,1.00) ~
BenchmarkGobEncode 39.5ms × (0.99,1.01) 37.8ms × (0.98,1.01) -4.33%
BenchmarkGzip 663ms × (1.00,1.01) 661ms × (0.99,1.01) ~
BenchmarkGunzip 143ms × (1.00,1.00) 142ms × (1.00,1.00) ~
BenchmarkHTTPClientServer 132µs × (0.99,1.01) 132µs × (0.99,1.01) ~
BenchmarkJSONEncode 57.4ms × (0.99,1.01) 56.3ms × (0.99,1.01) -1.96%
BenchmarkJSONDecode 139ms × (0.99,1.00) 138ms × (0.99,1.01) ~
BenchmarkMandelbrot200 6.03ms × (1.00,1.00) 6.01ms × (1.00,1.00) ~
BenchmarkGoParse 10.3ms × (0.89,1.14) 10.2ms × (0.87,1.05) ~
BenchmarkRegexpMatchEasy0_32 209ns × (1.00,1.00) 208ns × (1.00,1.00) ~
BenchmarkRegexpMatchEasy0_1K 591ns × (0.99,1.00) 588ns × (1.00,1.00) ~
BenchmarkRegexpMatchEasy1_32 184ns × (0.99,1.02) 182ns × (0.99,1.01) ~
BenchmarkRegexpMatchEasy1_1K 1.01µs × (1.00,1.00) 0.99µs × (1.00,1.01) -2.33%
BenchmarkRegexpMatchMedium_32 330ns × (1.00,1.00) 323ns × (1.00,1.01) -2.12%
BenchmarkRegexpMatchMedium_1K 92.6µs × (1.00,1.00) 89.9µs × (1.00,1.00) -2.92%
BenchmarkRegexpMatchHard_32 4.80µs × (0.95,1.00) 4.72µs × (0.95,1.01) ~
BenchmarkRegexpMatchHard_1K 136µs × (1.00,1.00) 133µs × (1.00,1.01) -1.86%
BenchmarkRevcomp 900ms × (0.99,1.04) 900ms × (1.00,1.05) ~
BenchmarkTemplate 172ms × (1.00,1.00) 168ms × (0.99,1.01) -2.07%
BenchmarkTimeParse 637ns × (1.00,1.00) 637ns × (1.00,1.00) ~
BenchmarkTimeFormat 744ns × (1.00,1.01) 738ns × (1.00,1.00) -0.67%
Change-Id: I4ecc925805da1f5ee264377f1f7574f54ee575e7
Reviewed-on: https://go-review.googlesource.com/9321
Reviewed-by: Austin Clements <austin@google.com>
2015-04-24 14:00:55 -04:00
|
|
|
const (
|
2015-06-25 12:24:44 -04:00
|
|
|
_GCoff = iota // GC not running; sweeping in background, write barrier disabled
|
2016-03-30 17:02:23 -04:00
|
|
|
_GCmark // GC marking roots and workbufs: allocate black, write barrier ENABLED
|
runtime: replace needwb() with writeBarrierEnabled
Reduce the write barrier check to a single load and compare
so that it can be inlined into write barrier use sites.
Makes the standard write barrier a little faster too.
name old new delta
BenchmarkBinaryTree17 17.9s × (0.99,1.01) 17.9s × (1.00,1.01) ~
BenchmarkFannkuch11 4.35s × (1.00,1.00) 4.43s × (1.00,1.00) +1.81%
BenchmarkFmtFprintfEmpty 120ns × (0.93,1.06) 110ns × (1.00,1.06) -7.92%
BenchmarkFmtFprintfString 479ns × (0.99,1.00) 487ns × (0.99,1.00) +1.67%
BenchmarkFmtFprintfInt 452ns × (0.99,1.02) 450ns × (0.99,1.00) ~
BenchmarkFmtFprintfIntInt 766ns × (0.99,1.01) 762ns × (1.00,1.00) ~
BenchmarkFmtFprintfPrefixedInt 576ns × (0.98,1.01) 584ns × (0.99,1.01) ~
BenchmarkFmtFprintfFloat 730ns × (1.00,1.01) 738ns × (1.00,1.00) +1.16%
BenchmarkFmtManyArgs 2.84µs × (0.99,1.00) 2.80µs × (1.00,1.01) -1.22%
BenchmarkGobDecode 39.3ms × (0.98,1.01) 39.0ms × (0.99,1.00) ~
BenchmarkGobEncode 39.5ms × (0.99,1.01) 37.8ms × (0.98,1.01) -4.33%
BenchmarkGzip 663ms × (1.00,1.01) 661ms × (0.99,1.01) ~
BenchmarkGunzip 143ms × (1.00,1.00) 142ms × (1.00,1.00) ~
BenchmarkHTTPClientServer 132µs × (0.99,1.01) 132µs × (0.99,1.01) ~
BenchmarkJSONEncode 57.4ms × (0.99,1.01) 56.3ms × (0.99,1.01) -1.96%
BenchmarkJSONDecode 139ms × (0.99,1.00) 138ms × (0.99,1.01) ~
BenchmarkMandelbrot200 6.03ms × (1.00,1.00) 6.01ms × (1.00,1.00) ~
BenchmarkGoParse 10.3ms × (0.89,1.14) 10.2ms × (0.87,1.05) ~
BenchmarkRegexpMatchEasy0_32 209ns × (1.00,1.00) 208ns × (1.00,1.00) ~
BenchmarkRegexpMatchEasy0_1K 591ns × (0.99,1.00) 588ns × (1.00,1.00) ~
BenchmarkRegexpMatchEasy1_32 184ns × (0.99,1.02) 182ns × (0.99,1.01) ~
BenchmarkRegexpMatchEasy1_1K 1.01µs × (1.00,1.00) 0.99µs × (1.00,1.01) -2.33%
BenchmarkRegexpMatchMedium_32 330ns × (1.00,1.00) 323ns × (1.00,1.01) -2.12%
BenchmarkRegexpMatchMedium_1K 92.6µs × (1.00,1.00) 89.9µs × (1.00,1.00) -2.92%
BenchmarkRegexpMatchHard_32 4.80µs × (0.95,1.00) 4.72µs × (0.95,1.01) ~
BenchmarkRegexpMatchHard_1K 136µs × (1.00,1.00) 133µs × (1.00,1.01) -1.86%
BenchmarkRevcomp 900ms × (0.99,1.04) 900ms × (1.00,1.05) ~
BenchmarkTemplate 172ms × (1.00,1.00) 168ms × (0.99,1.01) -2.07%
BenchmarkTimeParse 637ns × (1.00,1.00) 637ns × (1.00,1.00) ~
BenchmarkTimeFormat 744ns × (1.00,1.01) 738ns × (1.00,1.00) -0.67%
Change-Id: I4ecc925805da1f5ee264377f1f7574f54ee575e7
Reviewed-on: https://go-review.googlesource.com/9321
Reviewed-by: Austin Clements <austin@google.com>
2015-04-24 14:00:55 -04:00
|
|
|
_GCmarktermination // GC mark termination: allocate black, P's help GC, write barrier ENABLED
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
//go:nosplit
|
|
|
|
|
func setGCPhase(x uint32) {
|
2015-11-02 14:09:24 -05:00
|
|
|
atomic.Store(&gcphase, x)
|
2015-11-13 17:45:22 -08:00
|
|
|
writeBarrier.needed = gcphase == _GCmark || gcphase == _GCmarktermination
|
|
|
|
|
writeBarrier.enabled = writeBarrier.needed || writeBarrier.cgo
|
runtime: replace needwb() with writeBarrierEnabled
Reduce the write barrier check to a single load and compare
so that it can be inlined into write barrier use sites.
Makes the standard write barrier a little faster too.
name old new delta
BenchmarkBinaryTree17 17.9s × (0.99,1.01) 17.9s × (1.00,1.01) ~
BenchmarkFannkuch11 4.35s × (1.00,1.00) 4.43s × (1.00,1.00) +1.81%
BenchmarkFmtFprintfEmpty 120ns × (0.93,1.06) 110ns × (1.00,1.06) -7.92%
BenchmarkFmtFprintfString 479ns × (0.99,1.00) 487ns × (0.99,1.00) +1.67%
BenchmarkFmtFprintfInt 452ns × (0.99,1.02) 450ns × (0.99,1.00) ~
BenchmarkFmtFprintfIntInt 766ns × (0.99,1.01) 762ns × (1.00,1.00) ~
BenchmarkFmtFprintfPrefixedInt 576ns × (0.98,1.01) 584ns × (0.99,1.01) ~
BenchmarkFmtFprintfFloat 730ns × (1.00,1.01) 738ns × (1.00,1.00) +1.16%
BenchmarkFmtManyArgs 2.84µs × (0.99,1.00) 2.80µs × (1.00,1.01) -1.22%
BenchmarkGobDecode 39.3ms × (0.98,1.01) 39.0ms × (0.99,1.00) ~
BenchmarkGobEncode 39.5ms × (0.99,1.01) 37.8ms × (0.98,1.01) -4.33%
BenchmarkGzip 663ms × (1.00,1.01) 661ms × (0.99,1.01) ~
BenchmarkGunzip 143ms × (1.00,1.00) 142ms × (1.00,1.00) ~
BenchmarkHTTPClientServer 132µs × (0.99,1.01) 132µs × (0.99,1.01) ~
BenchmarkJSONEncode 57.4ms × (0.99,1.01) 56.3ms × (0.99,1.01) -1.96%
BenchmarkJSONDecode 139ms × (0.99,1.00) 138ms × (0.99,1.01) ~
BenchmarkMandelbrot200 6.03ms × (1.00,1.00) 6.01ms × (1.00,1.00) ~
BenchmarkGoParse 10.3ms × (0.89,1.14) 10.2ms × (0.87,1.05) ~
BenchmarkRegexpMatchEasy0_32 209ns × (1.00,1.00) 208ns × (1.00,1.00) ~
BenchmarkRegexpMatchEasy0_1K 591ns × (0.99,1.00) 588ns × (1.00,1.00) ~
BenchmarkRegexpMatchEasy1_32 184ns × (0.99,1.02) 182ns × (0.99,1.01) ~
BenchmarkRegexpMatchEasy1_1K 1.01µs × (1.00,1.00) 0.99µs × (1.00,1.01) -2.33%
BenchmarkRegexpMatchMedium_32 330ns × (1.00,1.00) 323ns × (1.00,1.01) -2.12%
BenchmarkRegexpMatchMedium_1K 92.6µs × (1.00,1.00) 89.9µs × (1.00,1.00) -2.92%
BenchmarkRegexpMatchHard_32 4.80µs × (0.95,1.00) 4.72µs × (0.95,1.01) ~
BenchmarkRegexpMatchHard_1K 136µs × (1.00,1.00) 133µs × (1.00,1.01) -1.86%
BenchmarkRevcomp 900ms × (0.99,1.04) 900ms × (1.00,1.05) ~
BenchmarkTemplate 172ms × (1.00,1.00) 168ms × (0.99,1.01) -2.07%
BenchmarkTimeParse 637ns × (1.00,1.00) 637ns × (1.00,1.00) ~
BenchmarkTimeFormat 744ns × (1.00,1.01) 738ns × (1.00,1.00) -0.67%
Change-Id: I4ecc925805da1f5ee264377f1f7574f54ee575e7
Reviewed-on: https://go-review.googlesource.com/9321
Reviewed-by: Austin Clements <austin@google.com>
2015-04-24 14:00:55 -04:00
|
|
|
}
|
|
|
|
|
|
2015-04-15 17:01:30 -04:00
|
|
|
// gcMarkWorkerMode represents the mode that a concurrent mark worker
|
|
|
|
|
// should operate in.
|
|
|
|
|
//
|
|
|
|
|
// Concurrent marking happens through four different mechanisms. One
|
|
|
|
|
// is mutator assists, which happen in response to allocations and are
|
|
|
|
|
// not scheduled. The other three are variations in the per-P mark
|
|
|
|
|
// workers and are distinguished by gcMarkWorkerMode.
|
|
|
|
|
type gcMarkWorkerMode int
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
// gcMarkWorkerDedicatedMode indicates that the P of a mark
|
|
|
|
|
// worker is dedicated to running that mark worker. The mark
|
runtime: eliminate getfull barrier from concurrent mark
Currently dedicated mark workers participate in the getfull barrier
during concurrent mark. However, the getfull barrier wasn't designed
for concurrent work and this causes no end of headaches.
In the concurrent setting, participants come and go. This makes mark
completion susceptible to live-lock: since dedicated workers are only
periodically polling for completion, it's possible for the program to
be in some transient worker each time one of the dedicated workers
wakes up to check if it can exit the getfull barrier. It also
complicates reasoning about the system because dedicated workers
participate directly in the getfull barrier, but transient workers
must instead use trygetfull because they have exit conditions that
aren't captured by getfull (e.g., fractional workers exit when
preempted). The complexity of implementing these exit conditions
contributed to #11677. Furthermore, the getfull barrier is inefficient
because we could be running user code instead of spinning on a P. In
effect, we're dedicating 25% of the CPU to marking even if that means
we have to spin to make that 25%. It also causes issues on Windows
because we can't actually sleep for 100µs (#8687).
Fix this by making dedicated workers no longer participate in the
getfull barrier. Instead, dedicated workers simply return to the
scheduler when they fail to get more work, regardless of what others
workers are doing, and the scheduler only starts new dedicated workers
if there's work available. Everything that needs to be handled by this
barrier is already handled by detection of mark completion.
This makes the system much more symmetric because all workers and
assists now use trygetfull during concurrent mark. It also loosens the
25% CPU target so that we can give some of that 25% back to user code
if there isn't enough work to keep the mark worker busy. And it
eliminates the problematic 100µs sleep on Windows during concurrent
mark (though not during mark termination).
The downside of this is that if we hit a bottleneck in the heap graph
that then expands back out, the system may shut down dedicated workers
and take a while to start them back up. We'll address this in the next
commit.
Updates #12041 and #8687.
No effect on the go1 benchmarks. This slows down the garbage benchmark
by 9%, but we'll more than make it up in the next commit.
name old time/op new time/op delta
XBenchGarbage-12 5.80ms ± 2% 6.32ms ± 4% +9.03% (p=0.000 n=20+20)
Change-Id: I65100a9ba005a8b5cf97940798918672ea9dd09b
Reviewed-on: https://go-review.googlesource.com/16297
Reviewed-by: Rick Hudson <rlh@golang.org>
2015-10-26 16:29:25 -04:00
|
|
|
// worker should run without preemption.
|
2015-04-15 17:01:30 -04:00
|
|
|
gcMarkWorkerDedicatedMode gcMarkWorkerMode = iota
|
|
|
|
|
|
|
|
|
|
// gcMarkWorkerFractionalMode indicates that a P is currently
|
|
|
|
|
// running the "fractional" mark worker. The fractional worker
|
2017-10-04 17:12:28 -04:00
|
|
|
// is necessary when GOMAXPROCS*gcBackgroundUtilization is not
|
|
|
|
|
// an integer. The fractional worker should run until it is
|
2015-04-15 17:01:30 -04:00
|
|
|
// preempted and will be scheduled to pick up the fractional
|
2017-10-04 17:12:28 -04:00
|
|
|
// part of GOMAXPROCS*gcBackgroundUtilization.
|
2015-04-15 17:01:30 -04:00
|
|
|
gcMarkWorkerFractionalMode
|
|
|
|
|
|
|
|
|
|
// gcMarkWorkerIdleMode indicates that a P is running the mark
|
|
|
|
|
// worker because it has nothing else to do. The idle worker
|
|
|
|
|
// should run until it is preempted and account its time
|
|
|
|
|
// against gcController.idleMarkTime.
|
|
|
|
|
gcMarkWorkerIdleMode
|
|
|
|
|
)
|
|
|
|
|
|
2016-10-07 17:25:26 -04:00
|
|
|
// gcMarkWorkerModeStrings are the strings labels of gcMarkWorkerModes
|
|
|
|
|
// to use in execution traces.
|
|
|
|
|
var gcMarkWorkerModeStrings = [...]string{
|
|
|
|
|
"GC (dedicated)",
|
|
|
|
|
"GC (fractional)",
|
|
|
|
|
"GC (idle)",
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-12 12:08:47 -04:00
|
|
|
// gcController implements the GC pacing controller that determines
|
|
|
|
|
// when to trigger concurrent garbage collection and how much marking
|
|
|
|
|
// work to do in mutator assists and background marking.
|
|
|
|
|
//
|
2016-09-15 14:08:04 -04:00
|
|
|
// It uses a feedback control algorithm to adjust the memstats.gc_trigger
|
2015-03-12 12:08:47 -04:00
|
|
|
// trigger based on the heap growth and GC CPU utilization each cycle.
|
|
|
|
|
// This algorithm optimizes for heap growth to match GOGC and for CPU
|
|
|
|
|
// utilization between assist and background marking to be 25% of
|
|
|
|
|
// GOMAXPROCS. The high-level design of this algorithm is documented
|
2015-07-10 17:17:11 -06:00
|
|
|
// at https://golang.org/s/go15gcpacing.
|
2017-03-31 17:09:41 -04:00
|
|
|
//
|
|
|
|
|
// All fields of gcController are used only during a single mark
|
|
|
|
|
// cycle.
|
|
|
|
|
var gcController gcControllerState
|
2015-03-12 12:08:47 -04:00
|
|
|
|
|
|
|
|
type gcControllerState struct {
|
|
|
|
|
// scanWork is the total scan work performed this cycle. This
|
2015-10-04 23:00:01 -04:00
|
|
|
// is updated atomically during the cycle. Updates occur in
|
|
|
|
|
// bounded batches, since it is both written and read
|
2016-04-16 18:27:38 -04:00
|
|
|
// throughout the cycle. At the end of the cycle, this is how
|
|
|
|
|
// much of the retained heap is scannable.
|
runtime: use heap scan size as estimate of GC scan work
Currently, the GC uses a moving average of recent scan work ratios to
estimate the total scan work required by this cycle. This is in turn
used to compute how much scan work should be done by mutators when
they allocate in order to perform all expected scan work by the time
the allocated heap reaches the heap goal.
However, our current scan work estimate can be arbitrarily wrong if
the heap topography changes significantly from one cycle to the
next. For example, in the go1 benchmarks, at the beginning of each
benchmark, the heap is dominated by a 256MB no-scan object, so the GC
learns that the scan density of the heap is very low. In benchmarks
that then rapidly allocate pointer-dense objects, by the time of the
next GC cycle, our estimate of the scan work can be too low by a large
factor. This in turn lets the mutator allocate faster than the GC can
collect, allowing it to get arbitrarily far ahead of the scan work
estimate, which leads to very long GC cycles with very little mutator
assist that can overshoot the heap goal by large margins. This is
particularly easy to demonstrate with BinaryTree17:
$ GODEBUG=gctrace=1 ./go1.test -test.bench BinaryTree17
gc #1 @0.017s 2%: 0+0+0+0+0 ms clock, 0+0+0+0/0/0+0 ms cpu, 4->262->262 MB, 4 MB goal, 1 P
gc #2 @0.026s 3%: 0+0+0+0+0 ms clock, 0+0+0+0/0/0+0 ms cpu, 262->262->262 MB, 524 MB goal, 1 P
testing: warning: no tests to run
PASS
BenchmarkBinaryTree17 gc #3 @1.906s 0%: 0+0+0+0+7 ms clock, 0+0+0+0/0/0+7 ms cpu, 325->325->287 MB, 325 MB goal, 1 P (forced)
gc #4 @12.203s 20%: 0+0+0+10067+10 ms clock, 0+0+0+0/2523/852+10 ms cpu, 430->2092->1950 MB, 574 MB goal, 1 P
1 9150447353 ns/op
Change this estimate to instead use the *current* scannable heap
size. This has the advantage of being based solely on the current
state of the heap, not on past densities or reachable heap sizes, so
it isn't susceptible to falling behind during these sorts of phase
changes. This is strictly an over-estimate, but it's better to
over-estimate and get more assist than necessary than it is to
under-estimate and potentially spiral out of control. Experiments with
scaling this estimate back showed no obvious benefit for mutator
utilization, heap size, or assist time.
This new estimate has little effect for most benchmarks, including
most go1 benchmarks, x/benchmarks, and the 6g benchmark. It has a huge
effect for benchmarks that triggered the bad pacer behavior:
name old mean new mean delta
BinaryTree17 10.0s × (1.00,1.00) 3.5s × (0.98,1.01) -64.93% (p=0.000)
Fannkuch11 2.74s × (1.00,1.01) 2.65s × (1.00,1.00) -3.52% (p=0.000)
FmtFprintfEmpty 56.4ns × (0.99,1.00) 57.8ns × (1.00,1.01) +2.43% (p=0.000)
FmtFprintfString 187ns × (0.99,1.00) 185ns × (0.99,1.01) -1.19% (p=0.010)
FmtFprintfInt 184ns × (1.00,1.00) 183ns × (1.00,1.00) (no variance)
FmtFprintfIntInt 321ns × (1.00,1.00) 315ns × (1.00,1.00) -1.80% (p=0.000)
FmtFprintfPrefixedInt 266ns × (1.00,1.00) 263ns × (1.00,1.00) -1.22% (p=0.000)
FmtFprintfFloat 353ns × (1.00,1.00) 353ns × (1.00,1.00) -0.13% (p=0.035)
FmtManyArgs 1.21µs × (1.00,1.00) 1.19µs × (1.00,1.00) -1.33% (p=0.000)
GobDecode 9.69ms × (1.00,1.00) 9.59ms × (1.00,1.00) -1.07% (p=0.000)
GobEncode 7.89ms × (0.99,1.01) 7.74ms × (1.00,1.00) -1.92% (p=0.000)
Gzip 391ms × (1.00,1.00) 392ms × (1.00,1.00) ~ (p=0.522)
Gunzip 97.1ms × (1.00,1.00) 97.0ms × (1.00,1.00) -0.10% (p=0.000)
HTTPClientServer 55.7µs × (0.99,1.01) 56.7µs × (0.99,1.01) +1.81% (p=0.001)
JSONEncode 19.1ms × (1.00,1.00) 19.0ms × (1.00,1.00) -0.85% (p=0.000)
JSONDecode 66.8ms × (1.00,1.00) 66.9ms × (1.00,1.00) ~ (p=0.288)
Mandelbrot200 4.13ms × (1.00,1.00) 4.12ms × (1.00,1.00) -0.08% (p=0.000)
GoParse 3.97ms × (1.00,1.01) 4.01ms × (1.00,1.00) +0.99% (p=0.000)
RegexpMatchEasy0_32 114ns × (1.00,1.00) 115ns × (0.99,1.00) ~ (p=0.070)
RegexpMatchEasy0_1K 376ns × (1.00,1.00) 376ns × (1.00,1.00) ~ (p=0.900)
RegexpMatchEasy1_32 94.9ns × (1.00,1.00) 96.3ns × (1.00,1.01) +1.53% (p=0.001)
RegexpMatchEasy1_1K 568ns × (1.00,1.00) 567ns × (1.00,1.00) -0.22% (p=0.001)
RegexpMatchMedium_32 159ns × (1.00,1.00) 159ns × (1.00,1.00) ~ (p=0.178)
RegexpMatchMedium_1K 46.4µs × (1.00,1.00) 46.6µs × (1.00,1.00) +0.29% (p=0.000)
RegexpMatchHard_32 2.37µs × (1.00,1.00) 2.37µs × (1.00,1.00) ~ (p=0.722)
RegexpMatchHard_1K 71.1µs × (1.00,1.00) 71.2µs × (1.00,1.00) ~ (p=0.229)
Revcomp 565ms × (1.00,1.00) 562ms × (1.00,1.00) -0.52% (p=0.000)
Template 81.0ms × (1.00,1.00) 80.2ms × (1.00,1.00) -0.97% (p=0.000)
TimeParse 380ns × (1.00,1.00) 380ns × (1.00,1.00) ~ (p=0.148)
TimeFormat 405ns × (0.99,1.00) 385ns × (0.99,1.00) -5.00% (p=0.000)
Change-Id: I11274158bf3affaf62662e02de7af12d5fb789e4
Reviewed-on: https://go-review.googlesource.com/9696
Reviewed-by: Russ Cox <rsc@golang.org>
Run-TryBot: Austin Clements <austin@google.com>
2015-05-04 16:55:31 -04:00
|
|
|
//
|
|
|
|
|
// Currently this is the bytes of heap scanned. For most uses,
|
|
|
|
|
// this is an opaque unit of work, but for estimation the
|
|
|
|
|
// definition is important.
|
2015-03-12 12:08:47 -04:00
|
|
|
scanWork int64
|
2015-03-12 17:56:14 -04:00
|
|
|
|
2015-03-13 13:29:23 -04:00
|
|
|
// bgScanCredit is the scan work credit accumulated by the
|
|
|
|
|
// concurrent background scan. This credit is accumulated by
|
|
|
|
|
// the background scan and stolen by mutator assists. This is
|
|
|
|
|
// updated atomically. Updates occur in bounded batches, since
|
|
|
|
|
// it is both written and read throughout the cycle.
|
|
|
|
|
bgScanCredit int64
|
|
|
|
|
|
2015-03-17 12:17:47 -04:00
|
|
|
// assistTime is the nanoseconds spent in mutator assists
|
|
|
|
|
// during this cycle. This is updated atomically. Updates
|
|
|
|
|
// occur in bounded batches, since it is both written and read
|
|
|
|
|
// throughout the cycle.
|
|
|
|
|
assistTime int64
|
|
|
|
|
|
2015-04-15 17:01:30 -04:00
|
|
|
// dedicatedMarkTime is the nanoseconds spent in dedicated
|
|
|
|
|
// mark workers during this cycle. This is updated atomically
|
|
|
|
|
// at the end of the concurrent mark phase.
|
|
|
|
|
dedicatedMarkTime int64
|
|
|
|
|
|
|
|
|
|
// fractionalMarkTime is the nanoseconds spent in the
|
|
|
|
|
// fractional mark worker during this cycle. This is updated
|
|
|
|
|
// atomically throughout the cycle and will be up-to-date if
|
|
|
|
|
// the fractional mark worker is not currently running.
|
|
|
|
|
fractionalMarkTime int64
|
runtime: multi-threaded, utilization-scheduled background mark
Currently, the concurrent mark phase is performed by the main GC
goroutine. Prior to the previous commit enabling preemption, this
caused marking to always consume 1/GOMAXPROCS of the available CPU
time. If GOMAXPROCS=1, this meant background GC would consume 100% of
the CPU (effectively a STW). If GOMAXPROCS>4, background GC would use
less than the goal of 25%. If GOMAXPROCS=4, background GC would use
the goal 25%, but if the mutator wasn't using the remaining 75%,
background marking wouldn't take advantage of the idle time. Enabling
preemption in the previous commit made GC miss CPU targets in
completely different ways, but set us up to bring everything back in
line.
This change replaces the fixed GC goroutine with per-P background mark
goroutines. Once started, these goroutines don't go in the standard
run queues; instead, they are scheduled specially such that the time
spent in mutator assists and the background mark goroutines totals 25%
of the CPU time available to the program. Furthermore, this lets
background marking take advantage of idle Ps, which significantly
boosts GC performance for applications that under-utilize the CPU.
This requires also changing how time is reported for gctrace, so this
change splits the concurrent mark CPU time into assist/background/idle
scanning.
This also requires increasing the size of the StackRecord slice used
in a GoroutineProfile test.
Change-Id: I0936ff907d2cee6cb687a208f2df47e8988e3157
Reviewed-on: https://go-review.googlesource.com/8850
Reviewed-by: Rick Hudson <rlh@golang.org>
2015-03-23 21:07:33 -04:00
|
|
|
|
|
|
|
|
// idleMarkTime is the nanoseconds spent in idle marking
|
2015-06-11 16:49:38 +03:00
|
|
|
// during this cycle. This is updated atomically throughout
|
runtime: multi-threaded, utilization-scheduled background mark
Currently, the concurrent mark phase is performed by the main GC
goroutine. Prior to the previous commit enabling preemption, this
caused marking to always consume 1/GOMAXPROCS of the available CPU
time. If GOMAXPROCS=1, this meant background GC would consume 100% of
the CPU (effectively a STW). If GOMAXPROCS>4, background GC would use
less than the goal of 25%. If GOMAXPROCS=4, background GC would use
the goal 25%, but if the mutator wasn't using the remaining 75%,
background marking wouldn't take advantage of the idle time. Enabling
preemption in the previous commit made GC miss CPU targets in
completely different ways, but set us up to bring everything back in
line.
This change replaces the fixed GC goroutine with per-P background mark
goroutines. Once started, these goroutines don't go in the standard
run queues; instead, they are scheduled specially such that the time
spent in mutator assists and the background mark goroutines totals 25%
of the CPU time available to the program. Furthermore, this lets
background marking take advantage of idle Ps, which significantly
boosts GC performance for applications that under-utilize the CPU.
This requires also changing how time is reported for gctrace, so this
change splits the concurrent mark CPU time into assist/background/idle
scanning.
This also requires increasing the size of the StackRecord slice used
in a GoroutineProfile test.
Change-Id: I0936ff907d2cee6cb687a208f2df47e8988e3157
Reviewed-on: https://go-review.googlesource.com/8850
Reviewed-by: Rick Hudson <rlh@golang.org>
2015-03-23 21:07:33 -04:00
|
|
|
// the cycle.
|
|
|
|
|
idleMarkTime int64
|
|
|
|
|
|
2016-03-02 17:27:59 -05:00
|
|
|
// markStartTime is the absolute start time in nanoseconds
|
|
|
|
|
// that assists and background mark workers started.
|
|
|
|
|
markStartTime int64
|
2015-08-03 18:06:05 -04:00
|
|
|
|
2015-04-15 17:01:30 -04:00
|
|
|
// dedicatedMarkWorkersNeeded is the number of dedicated mark
|
|
|
|
|
// workers that need to be started. This is computed at the
|
|
|
|
|
// beginning of each cycle and decremented atomically as
|
|
|
|
|
// dedicated mark workers get started.
|
|
|
|
|
dedicatedMarkWorkersNeeded int64
|
|
|
|
|
|
runtime: directly track GC assist balance
Currently we track the per-G GC assist balance as two monotonically
increasing values: the bytes allocated by the G this cycle (gcalloc)
and the scan work performed by the G this cycle (gcscanwork). The
assist balance is hence assistRatio*gcalloc - gcscanwork.
This works, but has two important downsides:
1) It requires floating-point math to figure out if a G is in debt or
not. This makes it inappropriate to check for assist debt in the
hot path of mallocgc, so we only do this when a G allocates a new
span. As a result, Gs can operate "in the red", leading to
under-assist and extended GC cycle length.
2) Revising the assist ratio during a GC cycle can lead to an "assist
burst". If you think of plotting the scan work performed versus
heaps size, the assist ratio controls the slope of this line.
However, in the current system, the target line always passes
through 0 at the heap size that triggered GC, so if the runtime
increases the assist ratio, there has to be a potentially large
assist to jump from the current amount of scan work up to the new
target scan work for the current heap size.
This commit replaces this approach with directly tracking the GC
assist balance in terms of allocation credit bytes. Allocating N bytes
simply decreases this by N and assisting raises it by the amount of
scan work performed divided by the assist ratio (to get back to
bytes).
This will make it cheap to figure out if a G is in debt, which will
let us efficiently check if an assist is necessary *before* performing
an allocation and hence keep Gs "in the black".
This also fixes assist bursts because the assist ratio is now in terms
of *remaining* work, rather than work from the beginning of the GC
cycle. Hence, the plot of scan work versus heap size becomes
continuous: we can revise the slope, but this slope always starts from
where we are right now, rather than where we were at the beginning of
the cycle.
Change-Id: Ia821c5f07f8a433e8da7f195b52adfedd58bdf2c
Reviewed-on: https://go-review.googlesource.com/15408
Reviewed-by: Rick Hudson <rlh@golang.org>
2015-10-04 20:16:57 -07:00
|
|
|
// assistWorkPerByte is the ratio of scan work to allocated
|
|
|
|
|
// bytes that should be performed by mutator assists. This is
|
runtime: revise assist ratio aggressively
At the start of a GC cycle, the garbage collector computes the assist
ratio based on the total scannable heap size. This was intended to be
conservative; after all, this assumes the entire heap may be reachable
and hence needs to be scanned. But it only assumes that the *current*
entire heap may be reachable. It fails to account for heap allocated
during the GC cycle. If the trigger ratio is very low (near zero), and
most of the heap is reachable when GC starts (which is likely if the
trigger ratio is near zero), then it's possible for the mutator to
create new, reachable heap fast enough that the assists won't keep up
based on the assist ratio computed at the beginning of the cycle. As a
result, the heap can grow beyond the heap goal (by hundreds of megs in
stress tests like in issue #11911).
We already have some vestigial logic for dealing with situations like
this; it just doesn't run often enough. Currently, every 10 ms during
the GC cycle, the GC revises the assist ratio. This was put in before
we switched to a conservative assist ratio (when we really were using
estimates of scannable heap), and it turns out to be exactly what we
need now. However, every 10 ms is far too infrequent for a rapidly
allocating mutator.
This commit reuses this logic, but replaces the 10 ms timer with
revising the assist ratio every time the heap is locked, which
coincides precisely with when the statistics used to compute the
assist ratio are updated.
Fixes #11911.
Change-Id: I377b231ab064946228378fa10422a46d1b50f4c5
Reviewed-on: https://go-review.googlesource.com/13047
Reviewed-by: Rick Hudson <rlh@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
2015-08-03 17:48:47 -04:00
|
|
|
// computed at the beginning of each cycle and updated every
|
|
|
|
|
// time heap_scan is updated.
|
runtime: directly track GC assist balance
Currently we track the per-G GC assist balance as two monotonically
increasing values: the bytes allocated by the G this cycle (gcalloc)
and the scan work performed by the G this cycle (gcscanwork). The
assist balance is hence assistRatio*gcalloc - gcscanwork.
This works, but has two important downsides:
1) It requires floating-point math to figure out if a G is in debt or
not. This makes it inappropriate to check for assist debt in the
hot path of mallocgc, so we only do this when a G allocates a new
span. As a result, Gs can operate "in the red", leading to
under-assist and extended GC cycle length.
2) Revising the assist ratio during a GC cycle can lead to an "assist
burst". If you think of plotting the scan work performed versus
heaps size, the assist ratio controls the slope of this line.
However, in the current system, the target line always passes
through 0 at the heap size that triggered GC, so if the runtime
increases the assist ratio, there has to be a potentially large
assist to jump from the current amount of scan work up to the new
target scan work for the current heap size.
This commit replaces this approach with directly tracking the GC
assist balance in terms of allocation credit bytes. Allocating N bytes
simply decreases this by N and assisting raises it by the amount of
scan work performed divided by the assist ratio (to get back to
bytes).
This will make it cheap to figure out if a G is in debt, which will
let us efficiently check if an assist is necessary *before* performing
an allocation and hence keep Gs "in the black".
This also fixes assist bursts because the assist ratio is now in terms
of *remaining* work, rather than work from the beginning of the GC
cycle. Hence, the plot of scan work versus heap size becomes
continuous: we can revise the slope, but this slope always starts from
where we are right now, rather than where we were at the beginning of
the cycle.
Change-Id: Ia821c5f07f8a433e8da7f195b52adfedd58bdf2c
Reviewed-on: https://go-review.googlesource.com/15408
Reviewed-by: Rick Hudson <rlh@golang.org>
2015-10-04 20:16:57 -07:00
|
|
|
assistWorkPerByte float64
|
|
|
|
|
|
|
|
|
|
// assistBytesPerWork is 1/assistWorkPerByte.
|
|
|
|
|
assistBytesPerWork float64
|
runtime: multi-threaded, utilization-scheduled background mark
Currently, the concurrent mark phase is performed by the main GC
goroutine. Prior to the previous commit enabling preemption, this
caused marking to always consume 1/GOMAXPROCS of the available CPU
time. If GOMAXPROCS=1, this meant background GC would consume 100% of
the CPU (effectively a STW). If GOMAXPROCS>4, background GC would use
less than the goal of 25%. If GOMAXPROCS=4, background GC would use
the goal 25%, but if the mutator wasn't using the remaining 75%,
background marking wouldn't take advantage of the idle time. Enabling
preemption in the previous commit made GC miss CPU targets in
completely different ways, but set us up to bring everything back in
line.
This change replaces the fixed GC goroutine with per-P background mark
goroutines. Once started, these goroutines don't go in the standard
run queues; instead, they are scheduled specially such that the time
spent in mutator assists and the background mark goroutines totals 25%
of the CPU time available to the program. Furthermore, this lets
background marking take advantage of idle Ps, which significantly
boosts GC performance for applications that under-utilize the CPU.
This requires also changing how time is reported for gctrace, so this
change splits the concurrent mark CPU time into assist/background/idle
scanning.
This also requires increasing the size of the StackRecord slice used
in a GoroutineProfile test.
Change-Id: I0936ff907d2cee6cb687a208f2df47e8988e3157
Reviewed-on: https://go-review.googlesource.com/8850
Reviewed-by: Rick Hudson <rlh@golang.org>
2015-03-23 21:07:33 -04:00
|
|
|
|
2015-04-15 17:01:30 -04:00
|
|
|
// fractionalUtilizationGoal is the fraction of wall clock
|
2017-10-05 12:16:45 -04:00
|
|
|
// time that should be spent in the fractional mark worker on
|
|
|
|
|
// each P that isn't running a dedicated worker.
|
|
|
|
|
//
|
|
|
|
|
// For example, if the utilization goal is 25% and there are
|
|
|
|
|
// no dedicated workers, this will be 0.25. If there goal is
|
|
|
|
|
// 25%, there is one dedicated worker, and GOMAXPROCS is 5,
|
|
|
|
|
// this will be 0.05 to make up the missing 5%.
|
|
|
|
|
//
|
|
|
|
|
// If this is zero, no fractional workers are needed.
|
2015-04-15 17:01:30 -04:00
|
|
|
fractionalUtilizationGoal float64
|
|
|
|
|
|
2015-11-11 12:39:30 -05:00
|
|
|
_ [sys.CacheLineSize]byte
|
2015-03-12 12:08:47 -04:00
|
|
|
}
|
|
|
|
|
|
2015-03-12 17:56:14 -04:00
|
|
|
// startCycle resets the GC controller's state and computes estimates
|
2015-03-17 12:17:47 -04:00
|
|
|
// for a new GC cycle. The caller must hold worldsema.
|
2015-03-12 12:08:47 -04:00
|
|
|
func (c *gcControllerState) startCycle() {
|
|
|
|
|
c.scanWork = 0
|
2015-03-13 13:29:23 -04:00
|
|
|
c.bgScanCredit = 0
|
2015-03-17 12:17:47 -04:00
|
|
|
c.assistTime = 0
|
2015-04-15 17:01:30 -04:00
|
|
|
c.dedicatedMarkTime = 0
|
|
|
|
|
c.fractionalMarkTime = 0
|
runtime: multi-threaded, utilization-scheduled background mark
Currently, the concurrent mark phase is performed by the main GC
goroutine. Prior to the previous commit enabling preemption, this
caused marking to always consume 1/GOMAXPROCS of the available CPU
time. If GOMAXPROCS=1, this meant background GC would consume 100% of
the CPU (effectively a STW). If GOMAXPROCS>4, background GC would use
less than the goal of 25%. If GOMAXPROCS=4, background GC would use
the goal 25%, but if the mutator wasn't using the remaining 75%,
background marking wouldn't take advantage of the idle time. Enabling
preemption in the previous commit made GC miss CPU targets in
completely different ways, but set us up to bring everything back in
line.
This change replaces the fixed GC goroutine with per-P background mark
goroutines. Once started, these goroutines don't go in the standard
run queues; instead, they are scheduled specially such that the time
spent in mutator assists and the background mark goroutines totals 25%
of the CPU time available to the program. Furthermore, this lets
background marking take advantage of idle Ps, which significantly
boosts GC performance for applications that under-utilize the CPU.
This requires also changing how time is reported for gctrace, so this
change splits the concurrent mark CPU time into assist/background/idle
scanning.
This also requires increasing the size of the StackRecord slice used
in a GoroutineProfile test.
Change-Id: I0936ff907d2cee6cb687a208f2df47e8988e3157
Reviewed-on: https://go-review.googlesource.com/8850
Reviewed-by: Rick Hudson <rlh@golang.org>
2015-03-23 21:07:33 -04:00
|
|
|
c.idleMarkTime = 0
|
2015-03-12 17:56:14 -04:00
|
|
|
|
|
|
|
|
// If this is the first GC cycle or we're operating on a very
|
2016-09-15 14:08:04 -04:00
|
|
|
// small heap, fake heap_marked so it looks like gc_trigger is
|
2015-03-12 17:56:14 -04:00
|
|
|
// the appropriate growth from heap_marked, even though the
|
|
|
|
|
// real heap_marked may not have a meaningful value (on the
|
|
|
|
|
// first cycle) or may be much smaller (resulting in a large
|
|
|
|
|
// error response).
|
2016-09-15 14:08:04 -04:00
|
|
|
if memstats.gc_trigger <= heapminimum {
|
2017-03-31 17:09:41 -04:00
|
|
|
memstats.heap_marked = uint64(float64(memstats.gc_trigger) / (1 + memstats.triggerRatio))
|
2015-03-12 17:56:14 -04:00
|
|
|
}
|
|
|
|
|
|
2016-09-15 14:08:04 -04:00
|
|
|
// Re-compute the heap goal for this cycle in case something
|
|
|
|
|
// changed. This is the same calculation we use elsewhere.
|
2016-09-15 14:30:31 -04:00
|
|
|
memstats.next_gc = memstats.heap_marked + memstats.heap_marked*uint64(gcpercent)/100
|
2016-09-29 11:46:53 -04:00
|
|
|
if gcpercent < 0 {
|
|
|
|
|
memstats.next_gc = ^uint64(0)
|
|
|
|
|
}
|
2015-03-17 12:17:47 -04:00
|
|
|
|
2015-10-07 22:37:15 -07:00
|
|
|
// Ensure that the heap goal is at least a little larger than
|
|
|
|
|
// the current live heap size. This may not be the case if GC
|
|
|
|
|
// start is delayed or if the allocation that pushed heap_live
|
2016-09-15 14:08:04 -04:00
|
|
|
// over gc_trigger is large or if the trigger is really close to
|
2015-10-07 22:37:15 -07:00
|
|
|
// GOGC. Assist is proportional to this distance, so enforce a
|
|
|
|
|
// minimum distance, even if it means going over the GOGC goal
|
|
|
|
|
// by a tiny bit.
|
2016-09-15 14:08:04 -04:00
|
|
|
if memstats.next_gc < memstats.heap_live+1024*1024 {
|
|
|
|
|
memstats.next_gc = memstats.heap_live + 1024*1024
|
2015-10-07 22:37:15 -07:00
|
|
|
}
|
|
|
|
|
|
2017-10-04 17:07:09 -04:00
|
|
|
// Compute the background mark utilization goal. In general,
|
|
|
|
|
// this may not come out exactly. We round the number of
|
|
|
|
|
// dedicated workers so that the utilization is closest to
|
|
|
|
|
// 25%. For small GOMAXPROCS, this would introduce too much
|
|
|
|
|
// error, so we add fractional workers in that case.
|
2017-10-04 17:12:28 -04:00
|
|
|
totalUtilizationGoal := float64(gomaxprocs) * gcBackgroundUtilization
|
2017-10-04 17:07:09 -04:00
|
|
|
c.dedicatedMarkWorkersNeeded = int64(totalUtilizationGoal + 0.5)
|
|
|
|
|
utilError := float64(c.dedicatedMarkWorkersNeeded)/totalUtilizationGoal - 1
|
|
|
|
|
const maxUtilError = 0.3
|
|
|
|
|
if utilError < -maxUtilError || utilError > maxUtilError {
|
|
|
|
|
// Rounding put us more than 30% off our goal. With
|
|
|
|
|
// gcBackgroundUtilization of 25%, this happens for
|
|
|
|
|
// GOMAXPROCS<=3 or GOMAXPROCS=6. Enable fractional
|
|
|
|
|
// workers to compensate.
|
|
|
|
|
if float64(c.dedicatedMarkWorkersNeeded) > totalUtilizationGoal {
|
|
|
|
|
// Too many dedicated workers.
|
|
|
|
|
c.dedicatedMarkWorkersNeeded--
|
|
|
|
|
}
|
2017-10-05 12:16:45 -04:00
|
|
|
c.fractionalUtilizationGoal = (totalUtilizationGoal - float64(c.dedicatedMarkWorkersNeeded)) / float64(gomaxprocs)
|
2017-10-04 17:07:09 -04:00
|
|
|
} else {
|
|
|
|
|
c.fractionalUtilizationGoal = 0
|
|
|
|
|
}
|
2015-04-15 17:01:30 -04:00
|
|
|
|
2015-03-17 12:17:47 -04:00
|
|
|
// Clear per-P state
|
2017-06-13 11:32:17 -04:00
|
|
|
for _, p := range allp {
|
2015-03-17 12:17:47 -04:00
|
|
|
p.gcAssistTime = 0
|
2017-10-05 12:16:45 -04:00
|
|
|
p.gcFractionalMarkTime = 0
|
2015-03-17 12:17:47 -04:00
|
|
|
}
|
|
|
|
|
|
2015-04-22 16:35:45 -04:00
|
|
|
// Compute initial values for controls that are updated
|
|
|
|
|
// throughout the cycle.
|
|
|
|
|
c.revise()
|
|
|
|
|
|
2015-08-03 17:45:44 -04:00
|
|
|
if debug.gcpacertrace > 0 {
|
runtime: directly track GC assist balance
Currently we track the per-G GC assist balance as two monotonically
increasing values: the bytes allocated by the G this cycle (gcalloc)
and the scan work performed by the G this cycle (gcscanwork). The
assist balance is hence assistRatio*gcalloc - gcscanwork.
This works, but has two important downsides:
1) It requires floating-point math to figure out if a G is in debt or
not. This makes it inappropriate to check for assist debt in the
hot path of mallocgc, so we only do this when a G allocates a new
span. As a result, Gs can operate "in the red", leading to
under-assist and extended GC cycle length.
2) Revising the assist ratio during a GC cycle can lead to an "assist
burst". If you think of plotting the scan work performed versus
heaps size, the assist ratio controls the slope of this line.
However, in the current system, the target line always passes
through 0 at the heap size that triggered GC, so if the runtime
increases the assist ratio, there has to be a potentially large
assist to jump from the current amount of scan work up to the new
target scan work for the current heap size.
This commit replaces this approach with directly tracking the GC
assist balance in terms of allocation credit bytes. Allocating N bytes
simply decreases this by N and assisting raises it by the amount of
scan work performed divided by the assist ratio (to get back to
bytes).
This will make it cheap to figure out if a G is in debt, which will
let us efficiently check if an assist is necessary *before* performing
an allocation and hence keep Gs "in the black".
This also fixes assist bursts because the assist ratio is now in terms
of *remaining* work, rather than work from the beginning of the GC
cycle. Hence, the plot of scan work versus heap size becomes
continuous: we can revise the slope, but this slope always starts from
where we are right now, rather than where we were at the beginning of
the cycle.
Change-Id: Ia821c5f07f8a433e8da7f195b52adfedd58bdf2c
Reviewed-on: https://go-review.googlesource.com/15408
Reviewed-by: Rick Hudson <rlh@golang.org>
2015-10-04 20:16:57 -07:00
|
|
|
print("pacer: assist ratio=", c.assistWorkPerByte,
|
2015-08-03 17:45:44 -04:00
|
|
|
" (scan ", memstats.heap_scan>>20, " MB in ",
|
|
|
|
|
work.initialHeapLive>>20, "->",
|
2016-09-15 14:08:04 -04:00
|
|
|
memstats.next_gc>>20, " MB)",
|
2015-08-03 17:45:44 -04:00
|
|
|
" workers=", c.dedicatedMarkWorkersNeeded,
|
2017-10-05 12:16:45 -04:00
|
|
|
"+", c.fractionalUtilizationGoal, "\n")
|
2015-08-03 17:45:44 -04:00
|
|
|
}
|
2015-03-12 17:56:14 -04:00
|
|
|
}
|
|
|
|
|
|
2015-04-17 16:26:55 -04:00
|
|
|
// revise updates the assist ratio during the GC cycle to account for
|
runtime: revise assist ratio aggressively
At the start of a GC cycle, the garbage collector computes the assist
ratio based on the total scannable heap size. This was intended to be
conservative; after all, this assumes the entire heap may be reachable
and hence needs to be scanned. But it only assumes that the *current*
entire heap may be reachable. It fails to account for heap allocated
during the GC cycle. If the trigger ratio is very low (near zero), and
most of the heap is reachable when GC starts (which is likely if the
trigger ratio is near zero), then it's possible for the mutator to
create new, reachable heap fast enough that the assists won't keep up
based on the assist ratio computed at the beginning of the cycle. As a
result, the heap can grow beyond the heap goal (by hundreds of megs in
stress tests like in issue #11911).
We already have some vestigial logic for dealing with situations like
this; it just doesn't run often enough. Currently, every 10 ms during
the GC cycle, the GC revises the assist ratio. This was put in before
we switched to a conservative assist ratio (when we really were using
estimates of scannable heap), and it turns out to be exactly what we
need now. However, every 10 ms is far too infrequent for a rapidly
allocating mutator.
This commit reuses this logic, but replaces the 10 ms timer with
revising the assist ratio every time the heap is locked, which
coincides precisely with when the statistics used to compute the
assist ratio are updated.
Fixes #11911.
Change-Id: I377b231ab064946228378fa10422a46d1b50f4c5
Reviewed-on: https://go-review.googlesource.com/13047
Reviewed-by: Rick Hudson <rlh@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
2015-08-03 17:48:47 -04:00
|
|
|
// improved estimates. This should be called either under STW or
|
2017-04-03 15:47:11 -04:00
|
|
|
// whenever memstats.heap_scan, memstats.heap_live, or
|
|
|
|
|
// memstats.next_gc is updated (with mheap_.lock held).
|
2015-10-07 22:37:15 -07:00
|
|
|
//
|
|
|
|
|
// It should only be called when gcBlackenEnabled != 0 (because this
|
|
|
|
|
// is when assists are enabled and the necessary statistics are
|
|
|
|
|
// available).
|
2015-04-17 16:26:55 -04:00
|
|
|
func (c *gcControllerState) revise() {
|
runtime: directly track GC assist balance
Currently we track the per-G GC assist balance as two monotonically
increasing values: the bytes allocated by the G this cycle (gcalloc)
and the scan work performed by the G this cycle (gcscanwork). The
assist balance is hence assistRatio*gcalloc - gcscanwork.
This works, but has two important downsides:
1) It requires floating-point math to figure out if a G is in debt or
not. This makes it inappropriate to check for assist debt in the
hot path of mallocgc, so we only do this when a G allocates a new
span. As a result, Gs can operate "in the red", leading to
under-assist and extended GC cycle length.
2) Revising the assist ratio during a GC cycle can lead to an "assist
burst". If you think of plotting the scan work performed versus
heaps size, the assist ratio controls the slope of this line.
However, in the current system, the target line always passes
through 0 at the heap size that triggered GC, so if the runtime
increases the assist ratio, there has to be a potentially large
assist to jump from the current amount of scan work up to the new
target scan work for the current heap size.
This commit replaces this approach with directly tracking the GC
assist balance in terms of allocation credit bytes. Allocating N bytes
simply decreases this by N and assisting raises it by the amount of
scan work performed divided by the assist ratio (to get back to
bytes).
This will make it cheap to figure out if a G is in debt, which will
let us efficiently check if an assist is necessary *before* performing
an allocation and hence keep Gs "in the black".
This also fixes assist bursts because the assist ratio is now in terms
of *remaining* work, rather than work from the beginning of the GC
cycle. Hence, the plot of scan work versus heap size becomes
continuous: we can revise the slope, but this slope always starts from
where we are right now, rather than where we were at the beginning of
the cycle.
Change-Id: Ia821c5f07f8a433e8da7f195b52adfedd58bdf2c
Reviewed-on: https://go-review.googlesource.com/15408
Reviewed-by: Rick Hudson <rlh@golang.org>
2015-10-04 20:16:57 -07:00
|
|
|
// Compute the expected scan work remaining.
|
runtime: use heap scan size as estimate of GC scan work
Currently, the GC uses a moving average of recent scan work ratios to
estimate the total scan work required by this cycle. This is in turn
used to compute how much scan work should be done by mutators when
they allocate in order to perform all expected scan work by the time
the allocated heap reaches the heap goal.
However, our current scan work estimate can be arbitrarily wrong if
the heap topography changes significantly from one cycle to the
next. For example, in the go1 benchmarks, at the beginning of each
benchmark, the heap is dominated by a 256MB no-scan object, so the GC
learns that the scan density of the heap is very low. In benchmarks
that then rapidly allocate pointer-dense objects, by the time of the
next GC cycle, our estimate of the scan work can be too low by a large
factor. This in turn lets the mutator allocate faster than the GC can
collect, allowing it to get arbitrarily far ahead of the scan work
estimate, which leads to very long GC cycles with very little mutator
assist that can overshoot the heap goal by large margins. This is
particularly easy to demonstrate with BinaryTree17:
$ GODEBUG=gctrace=1 ./go1.test -test.bench BinaryTree17
gc #1 @0.017s 2%: 0+0+0+0+0 ms clock, 0+0+0+0/0/0+0 ms cpu, 4->262->262 MB, 4 MB goal, 1 P
gc #2 @0.026s 3%: 0+0+0+0+0 ms clock, 0+0+0+0/0/0+0 ms cpu, 262->262->262 MB, 524 MB goal, 1 P
testing: warning: no tests to run
PASS
BenchmarkBinaryTree17 gc #3 @1.906s 0%: 0+0+0+0+7 ms clock, 0+0+0+0/0/0+7 ms cpu, 325->325->287 MB, 325 MB goal, 1 P (forced)
gc #4 @12.203s 20%: 0+0+0+10067+10 ms clock, 0+0+0+0/2523/852+10 ms cpu, 430->2092->1950 MB, 574 MB goal, 1 P
1 9150447353 ns/op
Change this estimate to instead use the *current* scannable heap
size. This has the advantage of being based solely on the current
state of the heap, not on past densities or reachable heap sizes, so
it isn't susceptible to falling behind during these sorts of phase
changes. This is strictly an over-estimate, but it's better to
over-estimate and get more assist than necessary than it is to
under-estimate and potentially spiral out of control. Experiments with
scaling this estimate back showed no obvious benefit for mutator
utilization, heap size, or assist time.
This new estimate has little effect for most benchmarks, including
most go1 benchmarks, x/benchmarks, and the 6g benchmark. It has a huge
effect for benchmarks that triggered the bad pacer behavior:
name old mean new mean delta
BinaryTree17 10.0s × (1.00,1.00) 3.5s × (0.98,1.01) -64.93% (p=0.000)
Fannkuch11 2.74s × (1.00,1.01) 2.65s × (1.00,1.00) -3.52% (p=0.000)
FmtFprintfEmpty 56.4ns × (0.99,1.00) 57.8ns × (1.00,1.01) +2.43% (p=0.000)
FmtFprintfString 187ns × (0.99,1.00) 185ns × (0.99,1.01) -1.19% (p=0.010)
FmtFprintfInt 184ns × (1.00,1.00) 183ns × (1.00,1.00) (no variance)
FmtFprintfIntInt 321ns × (1.00,1.00) 315ns × (1.00,1.00) -1.80% (p=0.000)
FmtFprintfPrefixedInt 266ns × (1.00,1.00) 263ns × (1.00,1.00) -1.22% (p=0.000)
FmtFprintfFloat 353ns × (1.00,1.00) 353ns × (1.00,1.00) -0.13% (p=0.035)
FmtManyArgs 1.21µs × (1.00,1.00) 1.19µs × (1.00,1.00) -1.33% (p=0.000)
GobDecode 9.69ms × (1.00,1.00) 9.59ms × (1.00,1.00) -1.07% (p=0.000)
GobEncode 7.89ms × (0.99,1.01) 7.74ms × (1.00,1.00) -1.92% (p=0.000)
Gzip 391ms × (1.00,1.00) 392ms × (1.00,1.00) ~ (p=0.522)
Gunzip 97.1ms × (1.00,1.00) 97.0ms × (1.00,1.00) -0.10% (p=0.000)
HTTPClientServer 55.7µs × (0.99,1.01) 56.7µs × (0.99,1.01) +1.81% (p=0.001)
JSONEncode 19.1ms × (1.00,1.00) 19.0ms × (1.00,1.00) -0.85% (p=0.000)
JSONDecode 66.8ms × (1.00,1.00) 66.9ms × (1.00,1.00) ~ (p=0.288)
Mandelbrot200 4.13ms × (1.00,1.00) 4.12ms × (1.00,1.00) -0.08% (p=0.000)
GoParse 3.97ms × (1.00,1.01) 4.01ms × (1.00,1.00) +0.99% (p=0.000)
RegexpMatchEasy0_32 114ns × (1.00,1.00) 115ns × (0.99,1.00) ~ (p=0.070)
RegexpMatchEasy0_1K 376ns × (1.00,1.00) 376ns × (1.00,1.00) ~ (p=0.900)
RegexpMatchEasy1_32 94.9ns × (1.00,1.00) 96.3ns × (1.00,1.01) +1.53% (p=0.001)
RegexpMatchEasy1_1K 568ns × (1.00,1.00) 567ns × (1.00,1.00) -0.22% (p=0.001)
RegexpMatchMedium_32 159ns × (1.00,1.00) 159ns × (1.00,1.00) ~ (p=0.178)
RegexpMatchMedium_1K 46.4µs × (1.00,1.00) 46.6µs × (1.00,1.00) +0.29% (p=0.000)
RegexpMatchHard_32 2.37µs × (1.00,1.00) 2.37µs × (1.00,1.00) ~ (p=0.722)
RegexpMatchHard_1K 71.1µs × (1.00,1.00) 71.2µs × (1.00,1.00) ~ (p=0.229)
Revcomp 565ms × (1.00,1.00) 562ms × (1.00,1.00) -0.52% (p=0.000)
Template 81.0ms × (1.00,1.00) 80.2ms × (1.00,1.00) -0.97% (p=0.000)
TimeParse 380ns × (1.00,1.00) 380ns × (1.00,1.00) ~ (p=0.148)
TimeFormat 405ns × (0.99,1.00) 385ns × (0.99,1.00) -5.00% (p=0.000)
Change-Id: I11274158bf3affaf62662e02de7af12d5fb789e4
Reviewed-on: https://go-review.googlesource.com/9696
Reviewed-by: Russ Cox <rsc@golang.org>
Run-TryBot: Austin Clements <austin@google.com>
2015-05-04 16:55:31 -04:00
|
|
|
//
|
2016-03-30 17:02:23 -04:00
|
|
|
// Note that we currently count allocations during GC as both
|
|
|
|
|
// scannable heap (heap_scan) and scan work completed
|
|
|
|
|
// (scanWork), so this difference won't be changed by
|
|
|
|
|
// allocations during GC.
|
2015-10-04 20:16:07 -07:00
|
|
|
//
|
|
|
|
|
// This particular estimate is a strict upper bound on the
|
runtime: directly track GC assist balance
Currently we track the per-G GC assist balance as two monotonically
increasing values: the bytes allocated by the G this cycle (gcalloc)
and the scan work performed by the G this cycle (gcscanwork). The
assist balance is hence assistRatio*gcalloc - gcscanwork.
This works, but has two important downsides:
1) It requires floating-point math to figure out if a G is in debt or
not. This makes it inappropriate to check for assist debt in the
hot path of mallocgc, so we only do this when a G allocates a new
span. As a result, Gs can operate "in the red", leading to
under-assist and extended GC cycle length.
2) Revising the assist ratio during a GC cycle can lead to an "assist
burst". If you think of plotting the scan work performed versus
heaps size, the assist ratio controls the slope of this line.
However, in the current system, the target line always passes
through 0 at the heap size that triggered GC, so if the runtime
increases the assist ratio, there has to be a potentially large
assist to jump from the current amount of scan work up to the new
target scan work for the current heap size.
This commit replaces this approach with directly tracking the GC
assist balance in terms of allocation credit bytes. Allocating N bytes
simply decreases this by N and assisting raises it by the amount of
scan work performed divided by the assist ratio (to get back to
bytes).
This will make it cheap to figure out if a G is in debt, which will
let us efficiently check if an assist is necessary *before* performing
an allocation and hence keep Gs "in the black".
This also fixes assist bursts because the assist ratio is now in terms
of *remaining* work, rather than work from the beginning of the GC
cycle. Hence, the plot of scan work versus heap size becomes
continuous: we can revise the slope, but this slope always starts from
where we are right now, rather than where we were at the beginning of
the cycle.
Change-Id: Ia821c5f07f8a433e8da7f195b52adfedd58bdf2c
Reviewed-on: https://go-review.googlesource.com/15408
Reviewed-by: Rick Hudson <rlh@golang.org>
2015-10-04 20:16:57 -07:00
|
|
|
// possible remaining scan work for the current heap.
|
runtime: use heap scan size as estimate of GC scan work
Currently, the GC uses a moving average of recent scan work ratios to
estimate the total scan work required by this cycle. This is in turn
used to compute how much scan work should be done by mutators when
they allocate in order to perform all expected scan work by the time
the allocated heap reaches the heap goal.
However, our current scan work estimate can be arbitrarily wrong if
the heap topography changes significantly from one cycle to the
next. For example, in the go1 benchmarks, at the beginning of each
benchmark, the heap is dominated by a 256MB no-scan object, so the GC
learns that the scan density of the heap is very low. In benchmarks
that then rapidly allocate pointer-dense objects, by the time of the
next GC cycle, our estimate of the scan work can be too low by a large
factor. This in turn lets the mutator allocate faster than the GC can
collect, allowing it to get arbitrarily far ahead of the scan work
estimate, which leads to very long GC cycles with very little mutator
assist that can overshoot the heap goal by large margins. This is
particularly easy to demonstrate with BinaryTree17:
$ GODEBUG=gctrace=1 ./go1.test -test.bench BinaryTree17
gc #1 @0.017s 2%: 0+0+0+0+0 ms clock, 0+0+0+0/0/0+0 ms cpu, 4->262->262 MB, 4 MB goal, 1 P
gc #2 @0.026s 3%: 0+0+0+0+0 ms clock, 0+0+0+0/0/0+0 ms cpu, 262->262->262 MB, 524 MB goal, 1 P
testing: warning: no tests to run
PASS
BenchmarkBinaryTree17 gc #3 @1.906s 0%: 0+0+0+0+7 ms clock, 0+0+0+0/0/0+7 ms cpu, 325->325->287 MB, 325 MB goal, 1 P (forced)
gc #4 @12.203s 20%: 0+0+0+10067+10 ms clock, 0+0+0+0/2523/852+10 ms cpu, 430->2092->1950 MB, 574 MB goal, 1 P
1 9150447353 ns/op
Change this estimate to instead use the *current* scannable heap
size. This has the advantage of being based solely on the current
state of the heap, not on past densities or reachable heap sizes, so
it isn't susceptible to falling behind during these sorts of phase
changes. This is strictly an over-estimate, but it's better to
over-estimate and get more assist than necessary than it is to
under-estimate and potentially spiral out of control. Experiments with
scaling this estimate back showed no obvious benefit for mutator
utilization, heap size, or assist time.
This new estimate has little effect for most benchmarks, including
most go1 benchmarks, x/benchmarks, and the 6g benchmark. It has a huge
effect for benchmarks that triggered the bad pacer behavior:
name old mean new mean delta
BinaryTree17 10.0s × (1.00,1.00) 3.5s × (0.98,1.01) -64.93% (p=0.000)
Fannkuch11 2.74s × (1.00,1.01) 2.65s × (1.00,1.00) -3.52% (p=0.000)
FmtFprintfEmpty 56.4ns × (0.99,1.00) 57.8ns × (1.00,1.01) +2.43% (p=0.000)
FmtFprintfString 187ns × (0.99,1.00) 185ns × (0.99,1.01) -1.19% (p=0.010)
FmtFprintfInt 184ns × (1.00,1.00) 183ns × (1.00,1.00) (no variance)
FmtFprintfIntInt 321ns × (1.00,1.00) 315ns × (1.00,1.00) -1.80% (p=0.000)
FmtFprintfPrefixedInt 266ns × (1.00,1.00) 263ns × (1.00,1.00) -1.22% (p=0.000)
FmtFprintfFloat 353ns × (1.00,1.00) 353ns × (1.00,1.00) -0.13% (p=0.035)
FmtManyArgs 1.21µs × (1.00,1.00) 1.19µs × (1.00,1.00) -1.33% (p=0.000)
GobDecode 9.69ms × (1.00,1.00) 9.59ms × (1.00,1.00) -1.07% (p=0.000)
GobEncode 7.89ms × (0.99,1.01) 7.74ms × (1.00,1.00) -1.92% (p=0.000)
Gzip 391ms × (1.00,1.00) 392ms × (1.00,1.00) ~ (p=0.522)
Gunzip 97.1ms × (1.00,1.00) 97.0ms × (1.00,1.00) -0.10% (p=0.000)
HTTPClientServer 55.7µs × (0.99,1.01) 56.7µs × (0.99,1.01) +1.81% (p=0.001)
JSONEncode 19.1ms × (1.00,1.00) 19.0ms × (1.00,1.00) -0.85% (p=0.000)
JSONDecode 66.8ms × (1.00,1.00) 66.9ms × (1.00,1.00) ~ (p=0.288)
Mandelbrot200 4.13ms × (1.00,1.00) 4.12ms × (1.00,1.00) -0.08% (p=0.000)
GoParse 3.97ms × (1.00,1.01) 4.01ms × (1.00,1.00) +0.99% (p=0.000)
RegexpMatchEasy0_32 114ns × (1.00,1.00) 115ns × (0.99,1.00) ~ (p=0.070)
RegexpMatchEasy0_1K 376ns × (1.00,1.00) 376ns × (1.00,1.00) ~ (p=0.900)
RegexpMatchEasy1_32 94.9ns × (1.00,1.00) 96.3ns × (1.00,1.01) +1.53% (p=0.001)
RegexpMatchEasy1_1K 568ns × (1.00,1.00) 567ns × (1.00,1.00) -0.22% (p=0.001)
RegexpMatchMedium_32 159ns × (1.00,1.00) 159ns × (1.00,1.00) ~ (p=0.178)
RegexpMatchMedium_1K 46.4µs × (1.00,1.00) 46.6µs × (1.00,1.00) +0.29% (p=0.000)
RegexpMatchHard_32 2.37µs × (1.00,1.00) 2.37µs × (1.00,1.00) ~ (p=0.722)
RegexpMatchHard_1K 71.1µs × (1.00,1.00) 71.2µs × (1.00,1.00) ~ (p=0.229)
Revcomp 565ms × (1.00,1.00) 562ms × (1.00,1.00) -0.52% (p=0.000)
Template 81.0ms × (1.00,1.00) 80.2ms × (1.00,1.00) -0.97% (p=0.000)
TimeParse 380ns × (1.00,1.00) 380ns × (1.00,1.00) ~ (p=0.148)
TimeFormat 405ns × (0.99,1.00) 385ns × (0.99,1.00) -5.00% (p=0.000)
Change-Id: I11274158bf3affaf62662e02de7af12d5fb789e4
Reviewed-on: https://go-review.googlesource.com/9696
Reviewed-by: Russ Cox <rsc@golang.org>
Run-TryBot: Austin Clements <austin@google.com>
2015-05-04 16:55:31 -04:00
|
|
|
// You might consider dividing this by 2 (or by
|
|
|
|
|
// (100+GOGC)/100) to counter this over-estimation, but
|
|
|
|
|
// benchmarks show that this has almost no effect on mean
|
|
|
|
|
// mutator utilization, heap size, or assist time and it
|
|
|
|
|
// introduces the danger of under-estimating and letting the
|
|
|
|
|
// mutator outpace the garbage collector.
|
runtime: directly track GC assist balance
Currently we track the per-G GC assist balance as two monotonically
increasing values: the bytes allocated by the G this cycle (gcalloc)
and the scan work performed by the G this cycle (gcscanwork). The
assist balance is hence assistRatio*gcalloc - gcscanwork.
This works, but has two important downsides:
1) It requires floating-point math to figure out if a G is in debt or
not. This makes it inappropriate to check for assist debt in the
hot path of mallocgc, so we only do this when a G allocates a new
span. As a result, Gs can operate "in the red", leading to
under-assist and extended GC cycle length.
2) Revising the assist ratio during a GC cycle can lead to an "assist
burst". If you think of plotting the scan work performed versus
heaps size, the assist ratio controls the slope of this line.
However, in the current system, the target line always passes
through 0 at the heap size that triggered GC, so if the runtime
increases the assist ratio, there has to be a potentially large
assist to jump from the current amount of scan work up to the new
target scan work for the current heap size.
This commit replaces this approach with directly tracking the GC
assist balance in terms of allocation credit bytes. Allocating N bytes
simply decreases this by N and assisting raises it by the amount of
scan work performed divided by the assist ratio (to get back to
bytes).
This will make it cheap to figure out if a G is in debt, which will
let us efficiently check if an assist is necessary *before* performing
an allocation and hence keep Gs "in the black".
This also fixes assist bursts because the assist ratio is now in terms
of *remaining* work, rather than work from the beginning of the GC
cycle. Hence, the plot of scan work versus heap size becomes
continuous: we can revise the slope, but this slope always starts from
where we are right now, rather than where we were at the beginning of
the cycle.
Change-Id: Ia821c5f07f8a433e8da7f195b52adfedd58bdf2c
Reviewed-on: https://go-review.googlesource.com/15408
Reviewed-by: Rick Hudson <rlh@golang.org>
2015-10-04 20:16:57 -07:00
|
|
|
scanWorkExpected := int64(memstats.heap_scan) - c.scanWork
|
|
|
|
|
if scanWorkExpected < 1000 {
|
|
|
|
|
// We set a somewhat arbitrary lower bound on
|
|
|
|
|
// remaining scan work since if we aim a little high,
|
|
|
|
|
// we can miss by a little.
|
|
|
|
|
//
|
|
|
|
|
// We *do* need to enforce that this is at least 1,
|
|
|
|
|
// since marking is racy and double-scanning objects
|
|
|
|
|
// may legitimately make the expected scan work
|
|
|
|
|
// negative.
|
|
|
|
|
scanWorkExpected = 1000
|
|
|
|
|
}
|
2015-04-17 16:26:55 -04:00
|
|
|
|
runtime: directly track GC assist balance
Currently we track the per-G GC assist balance as two monotonically
increasing values: the bytes allocated by the G this cycle (gcalloc)
and the scan work performed by the G this cycle (gcscanwork). The
assist balance is hence assistRatio*gcalloc - gcscanwork.
This works, but has two important downsides:
1) It requires floating-point math to figure out if a G is in debt or
not. This makes it inappropriate to check for assist debt in the
hot path of mallocgc, so we only do this when a G allocates a new
span. As a result, Gs can operate "in the red", leading to
under-assist and extended GC cycle length.
2) Revising the assist ratio during a GC cycle can lead to an "assist
burst". If you think of plotting the scan work performed versus
heaps size, the assist ratio controls the slope of this line.
However, in the current system, the target line always passes
through 0 at the heap size that triggered GC, so if the runtime
increases the assist ratio, there has to be a potentially large
assist to jump from the current amount of scan work up to the new
target scan work for the current heap size.
This commit replaces this approach with directly tracking the GC
assist balance in terms of allocation credit bytes. Allocating N bytes
simply decreases this by N and assisting raises it by the amount of
scan work performed divided by the assist ratio (to get back to
bytes).
This will make it cheap to figure out if a G is in debt, which will
let us efficiently check if an assist is necessary *before* performing
an allocation and hence keep Gs "in the black".
This also fixes assist bursts because the assist ratio is now in terms
of *remaining* work, rather than work from the beginning of the GC
cycle. Hence, the plot of scan work versus heap size becomes
continuous: we can revise the slope, but this slope always starts from
where we are right now, rather than where we were at the beginning of
the cycle.
Change-Id: Ia821c5f07f8a433e8da7f195b52adfedd58bdf2c
Reviewed-on: https://go-review.googlesource.com/15408
Reviewed-by: Rick Hudson <rlh@golang.org>
2015-10-04 20:16:57 -07:00
|
|
|
// Compute the heap distance remaining.
|
2017-04-21 11:45:44 -04:00
|
|
|
heapDistance := int64(memstats.next_gc) - int64(atomic.Load64(&memstats.heap_live))
|
2015-10-07 22:37:15 -07:00
|
|
|
if heapDistance <= 0 {
|
runtime: directly track GC assist balance
Currently we track the per-G GC assist balance as two monotonically
increasing values: the bytes allocated by the G this cycle (gcalloc)
and the scan work performed by the G this cycle (gcscanwork). The
assist balance is hence assistRatio*gcalloc - gcscanwork.
This works, but has two important downsides:
1) It requires floating-point math to figure out if a G is in debt or
not. This makes it inappropriate to check for assist debt in the
hot path of mallocgc, so we only do this when a G allocates a new
span. As a result, Gs can operate "in the red", leading to
under-assist and extended GC cycle length.
2) Revising the assist ratio during a GC cycle can lead to an "assist
burst". If you think of plotting the scan work performed versus
heaps size, the assist ratio controls the slope of this line.
However, in the current system, the target line always passes
through 0 at the heap size that triggered GC, so if the runtime
increases the assist ratio, there has to be a potentially large
assist to jump from the current amount of scan work up to the new
target scan work for the current heap size.
This commit replaces this approach with directly tracking the GC
assist balance in terms of allocation credit bytes. Allocating N bytes
simply decreases this by N and assisting raises it by the amount of
scan work performed divided by the assist ratio (to get back to
bytes).
This will make it cheap to figure out if a G is in debt, which will
let us efficiently check if an assist is necessary *before* performing
an allocation and hence keep Gs "in the black".
This also fixes assist bursts because the assist ratio is now in terms
of *remaining* work, rather than work from the beginning of the GC
cycle. Hence, the plot of scan work versus heap size becomes
continuous: we can revise the slope, but this slope always starts from
where we are right now, rather than where we were at the beginning of
the cycle.
Change-Id: Ia821c5f07f8a433e8da7f195b52adfedd58bdf2c
Reviewed-on: https://go-review.googlesource.com/15408
Reviewed-by: Rick Hudson <rlh@golang.org>
2015-10-04 20:16:57 -07:00
|
|
|
// This shouldn't happen, but if it does, avoid
|
|
|
|
|
// dividing by zero or setting the assist negative.
|
|
|
|
|
heapDistance = 1
|
2015-04-17 16:26:55 -04:00
|
|
|
}
|
runtime: directly track GC assist balance
Currently we track the per-G GC assist balance as two monotonically
increasing values: the bytes allocated by the G this cycle (gcalloc)
and the scan work performed by the G this cycle (gcscanwork). The
assist balance is hence assistRatio*gcalloc - gcscanwork.
This works, but has two important downsides:
1) It requires floating-point math to figure out if a G is in debt or
not. This makes it inappropriate to check for assist debt in the
hot path of mallocgc, so we only do this when a G allocates a new
span. As a result, Gs can operate "in the red", leading to
under-assist and extended GC cycle length.
2) Revising the assist ratio during a GC cycle can lead to an "assist
burst". If you think of plotting the scan work performed versus
heaps size, the assist ratio controls the slope of this line.
However, in the current system, the target line always passes
through 0 at the heap size that triggered GC, so if the runtime
increases the assist ratio, there has to be a potentially large
assist to jump from the current amount of scan work up to the new
target scan work for the current heap size.
This commit replaces this approach with directly tracking the GC
assist balance in terms of allocation credit bytes. Allocating N bytes
simply decreases this by N and assisting raises it by the amount of
scan work performed divided by the assist ratio (to get back to
bytes).
This will make it cheap to figure out if a G is in debt, which will
let us efficiently check if an assist is necessary *before* performing
an allocation and hence keep Gs "in the black".
This also fixes assist bursts because the assist ratio is now in terms
of *remaining* work, rather than work from the beginning of the GC
cycle. Hence, the plot of scan work versus heap size becomes
continuous: we can revise the slope, but this slope always starts from
where we are right now, rather than where we were at the beginning of
the cycle.
Change-Id: Ia821c5f07f8a433e8da7f195b52adfedd58bdf2c
Reviewed-on: https://go-review.googlesource.com/15408
Reviewed-by: Rick Hudson <rlh@golang.org>
2015-10-04 20:16:57 -07:00
|
|
|
|
|
|
|
|
// Compute the mutator assist ratio so by the time the mutator
|
|
|
|
|
// allocates the remaining heap bytes up to next_gc, it will
|
|
|
|
|
// have done (or stolen) the remaining amount of scan work.
|
|
|
|
|
c.assistWorkPerByte = float64(scanWorkExpected) / float64(heapDistance)
|
|
|
|
|
c.assistBytesPerWork = float64(heapDistance) / float64(scanWorkExpected)
|
2015-04-17 16:26:55 -04:00
|
|
|
}
|
|
|
|
|
|
2017-03-31 17:09:41 -04:00
|
|
|
// endCycle computes the trigger ratio for the next cycle.
|
|
|
|
|
func (c *gcControllerState) endCycle() float64 {
|
2017-03-30 14:59:53 -04:00
|
|
|
if work.userForced {
|
|
|
|
|
// Forced GC means this cycle didn't start at the
|
|
|
|
|
// trigger, so where it finished isn't good
|
|
|
|
|
// information about how to adjust the trigger.
|
2017-03-31 17:09:41 -04:00
|
|
|
// Just leave it where it is.
|
|
|
|
|
return memstats.triggerRatio
|
2017-03-30 14:59:53 -04:00
|
|
|
}
|
|
|
|
|
|
2015-03-24 10:45:20 -04:00
|
|
|
// Proportional response gain for the trigger controller. Must
|
|
|
|
|
// be in [0, 1]. Lower values smooth out transient effects but
|
|
|
|
|
// take longer to respond to phase changes. Higher values
|
|
|
|
|
// react to phase changes quickly, but are more affected by
|
|
|
|
|
// transient changes. Values near 1 may be unstable.
|
|
|
|
|
const triggerGain = 0.5
|
|
|
|
|
|
|
|
|
|
// Compute next cycle trigger ratio. First, this computes the
|
|
|
|
|
// "error" for this cycle; that is, how far off the trigger
|
|
|
|
|
// was from what it should have been, accounting for both heap
|
2015-08-03 18:09:13 -04:00
|
|
|
// growth and GC CPU utilization. We compute the actual heap
|
2015-03-24 10:45:20 -04:00
|
|
|
// growth during this cycle and scale that by how far off from
|
|
|
|
|
// the goal CPU utilization we were (to estimate the heap
|
|
|
|
|
// growth if we had the desired CPU utilization). The
|
|
|
|
|
// difference between this estimate and the GOGC-based goal
|
|
|
|
|
// heap growth is the error.
|
|
|
|
|
goalGrowthRatio := float64(gcpercent) / 100
|
|
|
|
|
actualGrowthRatio := float64(memstats.heap_live)/float64(memstats.heap_marked) - 1
|
2016-03-02 17:27:59 -05:00
|
|
|
assistDuration := nanotime() - c.markStartTime
|
runtime: schedule GC work more aggressively
Schedule the work as early as possible, while still respecting the
utilization percentage on average. The old code tried never to
go above the utilization percentage. The new code is willing
to go above the utilization percentage by one time slice
(but of course after doing that it must wait until the percentage
drops back down to the target before it gets another time slice).
The effect is that for concurrent GCs that can run in a small number
of time slices, the time during which write barriers are enabled is
reduced by one mutator + GC time slice round (possibly 30 ms per GC).
This only affects the fractional GC processor (the remainder of GOMAXPROCS/4),
so it matters most in GOMAXPROCS=1, a bit in GOMAXPROCS=2, and not at
all in GOMAXPROCS=4.
GOMAXPROCS=1
name old mean new mean delta
BenchmarkBinaryTree17 12.4s × (0.98,1.03) 13.5s × (0.97,1.04) +8.84% (p=0.000)
BenchmarkFannkuch11 4.38s × (1.00,1.01) 4.38s × (1.00,1.01) ~ (p=0.343)
BenchmarkFmtFprintfEmpty 88.9ns × (0.97,1.10) 90.1ns × (0.93,1.14) ~ (p=0.224)
BenchmarkFmtFprintfString 356ns × (0.94,1.05) 321ns × (0.94,1.12) -9.77% (p=0.000)
BenchmarkFmtFprintfInt 344ns × (0.98,1.03) 325ns × (0.96,1.03) -5.46% (p=0.000)
BenchmarkFmtFprintfIntInt 622ns × (0.97,1.03) 571ns × (0.95,1.05) -8.09% (p=0.000)
BenchmarkFmtFprintfPrefixedInt 462ns × (0.96,1.04) 431ns × (0.95,1.05) -6.81% (p=0.000)
BenchmarkFmtFprintfFloat 653ns × (0.98,1.03) 621ns × (0.99,1.03) -4.90% (p=0.000)
BenchmarkFmtManyArgs 2.32µs × (0.97,1.03) 2.19µs × (0.98,1.02) -5.43% (p=0.000)
BenchmarkGobDecode 27.0ms × (0.96,1.04) 20.0ms × (0.97,1.04) -26.06% (p=0.000)
BenchmarkGobEncode 26.6ms × (0.99,1.01) 17.8ms × (0.95,1.05) -33.19% (p=0.000)
BenchmarkGzip 659ms × (0.98,1.03) 650ms × (0.99,1.01) -1.34% (p=0.000)
BenchmarkGunzip 145ms × (0.98,1.04) 143ms × (1.00,1.01) -1.47% (p=0.000)
BenchmarkHTTPClientServer 111µs × (0.97,1.04) 110µs × (0.96,1.03) -1.30% (p=0.000)
BenchmarkJSONEncode 52.0ms × (0.97,1.03) 40.8ms × (0.97,1.03) -21.47% (p=0.000)
BenchmarkJSONDecode 127ms × (0.98,1.04) 120ms × (0.98,1.02) -5.55% (p=0.000)
BenchmarkMandelbrot200 6.04ms × (0.99,1.04) 6.02ms × (1.00,1.01) ~ (p=0.176)
BenchmarkGoParse 8.62ms × (0.96,1.08) 8.55ms × (0.93,1.09) ~ (p=0.302)
BenchmarkRegexpMatchEasy0_32 164ns × (0.98,1.05) 165ns × (0.98,1.07) ~ (p=0.293)
BenchmarkRegexpMatchEasy0_1K 546ns × (0.98,1.06) 547ns × (0.97,1.07) ~ (p=0.741)
BenchmarkRegexpMatchEasy1_32 142ns × (0.97,1.09) 141ns × (0.97,1.05) ~ (p=0.231)
BenchmarkRegexpMatchEasy1_1K 904ns × (0.97,1.07) 900ns × (0.98,1.04) ~ (p=0.294)
BenchmarkRegexpMatchMedium_32 256ns × (0.98,1.06) 256ns × (0.97,1.04) ~ (p=0.530)
BenchmarkRegexpMatchMedium_1K 74.2µs × (0.98,1.05) 73.8µs × (0.98,1.04) ~ (p=0.334)
BenchmarkRegexpMatchHard_32 3.94µs × (0.98,1.07) 3.92µs × (0.98,1.05) ~ (p=0.356)
BenchmarkRegexpMatchHard_1K 119µs × (0.98,1.07) 119µs × (0.98,1.06) ~ (p=0.467)
BenchmarkRevcomp 978ms × (0.96,1.09) 984ms × (0.95,1.07) ~ (p=0.448)
BenchmarkTemplate 151ms × (0.96,1.03) 142ms × (0.95,1.04) -5.55% (p=0.000)
BenchmarkTimeParse 628ns × (0.99,1.01) 628ns × (0.99,1.01) ~ (p=0.855)
BenchmarkTimeFormat 729ns × (0.98,1.06) 734ns × (0.97,1.05) ~ (p=0.149)
GOMAXPROCS=2
name old mean new mean delta
BenchmarkBinaryTree17-2 9.80s × (0.97,1.03) 9.85s × (0.99,1.02) ~ (p=0.444)
BenchmarkFannkuch11-2 4.35s × (0.99,1.01) 4.40s × (0.98,1.05) ~ (p=0.099)
BenchmarkFmtFprintfEmpty-2 86.7ns × (0.97,1.05) 85.9ns × (0.98,1.04) ~ (p=0.409)
BenchmarkFmtFprintfString-2 297ns × (0.98,1.01) 297ns × (0.99,1.01) ~ (p=0.743)
BenchmarkFmtFprintfInt-2 309ns × (0.98,1.02) 310ns × (0.99,1.01) ~ (p=0.464)
BenchmarkFmtFprintfIntInt-2 525ns × (0.97,1.05) 518ns × (0.99,1.01) ~ (p=0.151)
BenchmarkFmtFprintfPrefixedInt-2 408ns × (0.98,1.02) 408ns × (0.98,1.03) ~ (p=0.797)
BenchmarkFmtFprintfFloat-2 603ns × (0.99,1.01) 604ns × (0.98,1.02) ~ (p=0.588)
BenchmarkFmtManyArgs-2 2.07µs × (0.98,1.02) 2.05µs × (0.99,1.01) ~ (p=0.091)
BenchmarkGobDecode-2 19.1ms × (0.97,1.01) 19.3ms × (0.97,1.04) ~ (p=0.195)
BenchmarkGobEncode-2 16.2ms × (0.97,1.03) 16.4ms × (0.99,1.01) ~ (p=0.069)
BenchmarkGzip-2 652ms × (0.99,1.01) 651ms × (0.99,1.01) ~ (p=0.705)
BenchmarkGunzip-2 143ms × (1.00,1.01) 143ms × (1.00,1.00) ~ (p=0.665)
BenchmarkHTTPClientServer-2 149µs × (0.92,1.11) 149µs × (0.91,1.08) ~ (p=0.862)
BenchmarkJSONEncode-2 34.6ms × (0.98,1.02) 37.2ms × (0.99,1.01) +7.56% (p=0.000)
BenchmarkJSONDecode-2 117ms × (0.99,1.01) 117ms × (0.99,1.01) ~ (p=0.858)
BenchmarkMandelbrot200-2 6.10ms × (0.99,1.03) 6.03ms × (1.00,1.00) ~ (p=0.083)
BenchmarkGoParse-2 8.25ms × (0.98,1.01) 8.21ms × (0.99,1.02) ~ (p=0.307)
BenchmarkRegexpMatchEasy0_32-2 162ns × (0.99,1.02) 162ns × (0.99,1.01) ~ (p=0.857)
BenchmarkRegexpMatchEasy0_1K-2 541ns × (0.99,1.01) 540ns × (1.00,1.00) ~ (p=0.530)
BenchmarkRegexpMatchEasy1_32-2 138ns × (1.00,1.00) 141ns × (0.98,1.04) +1.88% (p=0.038)
BenchmarkRegexpMatchEasy1_1K-2 887ns × (0.99,1.01) 894ns × (0.99,1.01) ~ (p=0.087)
BenchmarkRegexpMatchMedium_32-2 252ns × (0.99,1.01) 252ns × (0.99,1.01) ~ (p=0.954)
BenchmarkRegexpMatchMedium_1K-2 73.4µs × (0.99,1.02) 72.8µs × (1.00,1.01) -0.87% (p=0.029)
BenchmarkRegexpMatchHard_32-2 3.95µs × (0.97,1.05) 3.87µs × (1.00,1.01) -2.11% (p=0.035)
BenchmarkRegexpMatchHard_1K-2 117µs × (0.99,1.01) 117µs × (0.99,1.01) ~ (p=0.669)
BenchmarkRevcomp-2 980ms × (0.95,1.03) 993ms × (0.94,1.09) ~ (p=0.527)
BenchmarkTemplate-2 136ms × (0.98,1.01) 135ms × (0.99,1.01) ~ (p=0.200)
BenchmarkTimeParse-2 630ns × (1.00,1.01) 630ns × (1.00,1.00) ~ (p=0.634)
BenchmarkTimeFormat-2 705ns × (0.99,1.01) 710ns × (0.98,1.02) ~ (p=0.174)
GOMAXPROCS=4
BenchmarkBinaryTree17-4 9.87s × (0.96,1.04) 9.75s × (0.96,1.03) ~ (p=0.178)
BenchmarkFannkuch11-4 4.35s × (1.00,1.01) 4.40s × (0.99,1.04) ~ (p=0.071)
BenchmarkFmtFprintfEmpty-4 85.8ns × (0.98,1.06) 85.6ns × (0.98,1.04) ~ (p=0.858)
BenchmarkFmtFprintfString-4 306ns × (0.99,1.03) 304ns × (0.97,1.02) ~ (p=0.470)
BenchmarkFmtFprintfInt-4 317ns × (0.98,1.01) 315ns × (0.98,1.02) -0.92% (p=0.044)
BenchmarkFmtFprintfIntInt-4 527ns × (0.99,1.01) 525ns × (0.98,1.01) ~ (p=0.164)
BenchmarkFmtFprintfPrefixedInt-4 421ns × (0.98,1.03) 417ns × (0.99,1.02) ~ (p=0.092)
BenchmarkFmtFprintfFloat-4 623ns × (0.98,1.02) 618ns × (0.98,1.03) ~ (p=0.172)
BenchmarkFmtManyArgs-4 2.09µs × (0.98,1.02) 2.09µs × (0.98,1.02) ~ (p=0.679)
BenchmarkGobDecode-4 18.6ms × (0.99,1.01) 18.6ms × (0.98,1.03) ~ (p=0.595)
BenchmarkGobEncode-4 15.0ms × (0.98,1.02) 15.1ms × (0.99,1.01) ~ (p=0.301)
BenchmarkGzip-4 659ms × (0.98,1.04) 660ms × (0.97,1.02) ~ (p=0.724)
BenchmarkGunzip-4 145ms × (0.98,1.04) 144ms × (0.99,1.04) ~ (p=0.671)
BenchmarkHTTPClientServer-4 139µs × (0.97,1.02) 138µs × (0.99,1.02) ~ (p=0.392)
BenchmarkJSONEncode-4 35.0ms × (0.99,1.02) 35.1ms × (0.98,1.02) ~ (p=0.777)
BenchmarkJSONDecode-4 119ms × (0.98,1.01) 118ms × (0.98,1.02) ~ (p=0.710)
BenchmarkMandelbrot200-4 6.02ms × (1.00,1.00) 6.02ms × (1.00,1.00) ~ (p=0.289)
BenchmarkGoParse-4 7.96ms × (0.99,1.01) 7.96ms × (0.99,1.01) ~ (p=0.884)
BenchmarkRegexpMatchEasy0_32-4 164ns × (0.98,1.04) 166ns × (0.97,1.04) ~ (p=0.221)
BenchmarkRegexpMatchEasy0_1K-4 540ns × (0.99,1.01) 552ns × (0.97,1.04) +2.10% (p=0.018)
BenchmarkRegexpMatchEasy1_32-4 140ns × (0.99,1.04) 142ns × (0.97,1.04) ~ (p=0.226)
BenchmarkRegexpMatchEasy1_1K-4 896ns × (0.99,1.03) 907ns × (0.97,1.04) ~ (p=0.155)
BenchmarkRegexpMatchMedium_32-4 255ns × (0.99,1.04) 255ns × (0.98,1.04) ~ (p=0.904)
BenchmarkRegexpMatchMedium_1K-4 73.4µs × (0.99,1.04) 73.8µs × (0.98,1.04) ~ (p=0.560)
BenchmarkRegexpMatchHard_32-4 3.93µs × (0.98,1.04) 3.95µs × (0.98,1.04) ~ (p=0.571)
BenchmarkRegexpMatchHard_1K-4 117µs × (1.00,1.01) 119µs × (0.98,1.04) +1.48% (p=0.048)
BenchmarkRevcomp-4 990ms × (0.94,1.08) 989ms × (0.94,1.10) ~ (p=0.957)
BenchmarkTemplate-4 137ms × (0.98,1.02) 137ms × (0.99,1.01) ~ (p=0.996)
BenchmarkTimeParse-4 629ns × (1.00,1.00) 629ns × (0.99,1.01) ~ (p=0.924)
BenchmarkTimeFormat-4 710ns × (0.99,1.01) 716ns × (0.98,1.02) +0.84% (p=0.033)
Change-Id: I43a04e0f6ad5e3ba9847dddf12e13222561f9cf4
Reviewed-on: https://go-review.googlesource.com/9543
Reviewed-by: Austin Clements <austin@google.com>
2015-04-30 00:17:09 -04:00
|
|
|
|
|
|
|
|
// Assume background mark hit its utilization goal.
|
2017-10-04 17:12:28 -04:00
|
|
|
utilization := gcBackgroundUtilization
|
runtime: schedule GC work more aggressively
Schedule the work as early as possible, while still respecting the
utilization percentage on average. The old code tried never to
go above the utilization percentage. The new code is willing
to go above the utilization percentage by one time slice
(but of course after doing that it must wait until the percentage
drops back down to the target before it gets another time slice).
The effect is that for concurrent GCs that can run in a small number
of time slices, the time during which write barriers are enabled is
reduced by one mutator + GC time slice round (possibly 30 ms per GC).
This only affects the fractional GC processor (the remainder of GOMAXPROCS/4),
so it matters most in GOMAXPROCS=1, a bit in GOMAXPROCS=2, and not at
all in GOMAXPROCS=4.
GOMAXPROCS=1
name old mean new mean delta
BenchmarkBinaryTree17 12.4s × (0.98,1.03) 13.5s × (0.97,1.04) +8.84% (p=0.000)
BenchmarkFannkuch11 4.38s × (1.00,1.01) 4.38s × (1.00,1.01) ~ (p=0.343)
BenchmarkFmtFprintfEmpty 88.9ns × (0.97,1.10) 90.1ns × (0.93,1.14) ~ (p=0.224)
BenchmarkFmtFprintfString 356ns × (0.94,1.05) 321ns × (0.94,1.12) -9.77% (p=0.000)
BenchmarkFmtFprintfInt 344ns × (0.98,1.03) 325ns × (0.96,1.03) -5.46% (p=0.000)
BenchmarkFmtFprintfIntInt 622ns × (0.97,1.03) 571ns × (0.95,1.05) -8.09% (p=0.000)
BenchmarkFmtFprintfPrefixedInt 462ns × (0.96,1.04) 431ns × (0.95,1.05) -6.81% (p=0.000)
BenchmarkFmtFprintfFloat 653ns × (0.98,1.03) 621ns × (0.99,1.03) -4.90% (p=0.000)
BenchmarkFmtManyArgs 2.32µs × (0.97,1.03) 2.19µs × (0.98,1.02) -5.43% (p=0.000)
BenchmarkGobDecode 27.0ms × (0.96,1.04) 20.0ms × (0.97,1.04) -26.06% (p=0.000)
BenchmarkGobEncode 26.6ms × (0.99,1.01) 17.8ms × (0.95,1.05) -33.19% (p=0.000)
BenchmarkGzip 659ms × (0.98,1.03) 650ms × (0.99,1.01) -1.34% (p=0.000)
BenchmarkGunzip 145ms × (0.98,1.04) 143ms × (1.00,1.01) -1.47% (p=0.000)
BenchmarkHTTPClientServer 111µs × (0.97,1.04) 110µs × (0.96,1.03) -1.30% (p=0.000)
BenchmarkJSONEncode 52.0ms × (0.97,1.03) 40.8ms × (0.97,1.03) -21.47% (p=0.000)
BenchmarkJSONDecode 127ms × (0.98,1.04) 120ms × (0.98,1.02) -5.55% (p=0.000)
BenchmarkMandelbrot200 6.04ms × (0.99,1.04) 6.02ms × (1.00,1.01) ~ (p=0.176)
BenchmarkGoParse 8.62ms × (0.96,1.08) 8.55ms × (0.93,1.09) ~ (p=0.302)
BenchmarkRegexpMatchEasy0_32 164ns × (0.98,1.05) 165ns × (0.98,1.07) ~ (p=0.293)
BenchmarkRegexpMatchEasy0_1K 546ns × (0.98,1.06) 547ns × (0.97,1.07) ~ (p=0.741)
BenchmarkRegexpMatchEasy1_32 142ns × (0.97,1.09) 141ns × (0.97,1.05) ~ (p=0.231)
BenchmarkRegexpMatchEasy1_1K 904ns × (0.97,1.07) 900ns × (0.98,1.04) ~ (p=0.294)
BenchmarkRegexpMatchMedium_32 256ns × (0.98,1.06) 256ns × (0.97,1.04) ~ (p=0.530)
BenchmarkRegexpMatchMedium_1K 74.2µs × (0.98,1.05) 73.8µs × (0.98,1.04) ~ (p=0.334)
BenchmarkRegexpMatchHard_32 3.94µs × (0.98,1.07) 3.92µs × (0.98,1.05) ~ (p=0.356)
BenchmarkRegexpMatchHard_1K 119µs × (0.98,1.07) 119µs × (0.98,1.06) ~ (p=0.467)
BenchmarkRevcomp 978ms × (0.96,1.09) 984ms × (0.95,1.07) ~ (p=0.448)
BenchmarkTemplate 151ms × (0.96,1.03) 142ms × (0.95,1.04) -5.55% (p=0.000)
BenchmarkTimeParse 628ns × (0.99,1.01) 628ns × (0.99,1.01) ~ (p=0.855)
BenchmarkTimeFormat 729ns × (0.98,1.06) 734ns × (0.97,1.05) ~ (p=0.149)
GOMAXPROCS=2
name old mean new mean delta
BenchmarkBinaryTree17-2 9.80s × (0.97,1.03) 9.85s × (0.99,1.02) ~ (p=0.444)
BenchmarkFannkuch11-2 4.35s × (0.99,1.01) 4.40s × (0.98,1.05) ~ (p=0.099)
BenchmarkFmtFprintfEmpty-2 86.7ns × (0.97,1.05) 85.9ns × (0.98,1.04) ~ (p=0.409)
BenchmarkFmtFprintfString-2 297ns × (0.98,1.01) 297ns × (0.99,1.01) ~ (p=0.743)
BenchmarkFmtFprintfInt-2 309ns × (0.98,1.02) 310ns × (0.99,1.01) ~ (p=0.464)
BenchmarkFmtFprintfIntInt-2 525ns × (0.97,1.05) 518ns × (0.99,1.01) ~ (p=0.151)
BenchmarkFmtFprintfPrefixedInt-2 408ns × (0.98,1.02) 408ns × (0.98,1.03) ~ (p=0.797)
BenchmarkFmtFprintfFloat-2 603ns × (0.99,1.01) 604ns × (0.98,1.02) ~ (p=0.588)
BenchmarkFmtManyArgs-2 2.07µs × (0.98,1.02) 2.05µs × (0.99,1.01) ~ (p=0.091)
BenchmarkGobDecode-2 19.1ms × (0.97,1.01) 19.3ms × (0.97,1.04) ~ (p=0.195)
BenchmarkGobEncode-2 16.2ms × (0.97,1.03) 16.4ms × (0.99,1.01) ~ (p=0.069)
BenchmarkGzip-2 652ms × (0.99,1.01) 651ms × (0.99,1.01) ~ (p=0.705)
BenchmarkGunzip-2 143ms × (1.00,1.01) 143ms × (1.00,1.00) ~ (p=0.665)
BenchmarkHTTPClientServer-2 149µs × (0.92,1.11) 149µs × (0.91,1.08) ~ (p=0.862)
BenchmarkJSONEncode-2 34.6ms × (0.98,1.02) 37.2ms × (0.99,1.01) +7.56% (p=0.000)
BenchmarkJSONDecode-2 117ms × (0.99,1.01) 117ms × (0.99,1.01) ~ (p=0.858)
BenchmarkMandelbrot200-2 6.10ms × (0.99,1.03) 6.03ms × (1.00,1.00) ~ (p=0.083)
BenchmarkGoParse-2 8.25ms × (0.98,1.01) 8.21ms × (0.99,1.02) ~ (p=0.307)
BenchmarkRegexpMatchEasy0_32-2 162ns × (0.99,1.02) 162ns × (0.99,1.01) ~ (p=0.857)
BenchmarkRegexpMatchEasy0_1K-2 541ns × (0.99,1.01) 540ns × (1.00,1.00) ~ (p=0.530)
BenchmarkRegexpMatchEasy1_32-2 138ns × (1.00,1.00) 141ns × (0.98,1.04) +1.88% (p=0.038)
BenchmarkRegexpMatchEasy1_1K-2 887ns × (0.99,1.01) 894ns × (0.99,1.01) ~ (p=0.087)
BenchmarkRegexpMatchMedium_32-2 252ns × (0.99,1.01) 252ns × (0.99,1.01) ~ (p=0.954)
BenchmarkRegexpMatchMedium_1K-2 73.4µs × (0.99,1.02) 72.8µs × (1.00,1.01) -0.87% (p=0.029)
BenchmarkRegexpMatchHard_32-2 3.95µs × (0.97,1.05) 3.87µs × (1.00,1.01) -2.11% (p=0.035)
BenchmarkRegexpMatchHard_1K-2 117µs × (0.99,1.01) 117µs × (0.99,1.01) ~ (p=0.669)
BenchmarkRevcomp-2 980ms × (0.95,1.03) 993ms × (0.94,1.09) ~ (p=0.527)
BenchmarkTemplate-2 136ms × (0.98,1.01) 135ms × (0.99,1.01) ~ (p=0.200)
BenchmarkTimeParse-2 630ns × (1.00,1.01) 630ns × (1.00,1.00) ~ (p=0.634)
BenchmarkTimeFormat-2 705ns × (0.99,1.01) 710ns × (0.98,1.02) ~ (p=0.174)
GOMAXPROCS=4
BenchmarkBinaryTree17-4 9.87s × (0.96,1.04) 9.75s × (0.96,1.03) ~ (p=0.178)
BenchmarkFannkuch11-4 4.35s × (1.00,1.01) 4.40s × (0.99,1.04) ~ (p=0.071)
BenchmarkFmtFprintfEmpty-4 85.8ns × (0.98,1.06) 85.6ns × (0.98,1.04) ~ (p=0.858)
BenchmarkFmtFprintfString-4 306ns × (0.99,1.03) 304ns × (0.97,1.02) ~ (p=0.470)
BenchmarkFmtFprintfInt-4 317ns × (0.98,1.01) 315ns × (0.98,1.02) -0.92% (p=0.044)
BenchmarkFmtFprintfIntInt-4 527ns × (0.99,1.01) 525ns × (0.98,1.01) ~ (p=0.164)
BenchmarkFmtFprintfPrefixedInt-4 421ns × (0.98,1.03) 417ns × (0.99,1.02) ~ (p=0.092)
BenchmarkFmtFprintfFloat-4 623ns × (0.98,1.02) 618ns × (0.98,1.03) ~ (p=0.172)
BenchmarkFmtManyArgs-4 2.09µs × (0.98,1.02) 2.09µs × (0.98,1.02) ~ (p=0.679)
BenchmarkGobDecode-4 18.6ms × (0.99,1.01) 18.6ms × (0.98,1.03) ~ (p=0.595)
BenchmarkGobEncode-4 15.0ms × (0.98,1.02) 15.1ms × (0.99,1.01) ~ (p=0.301)
BenchmarkGzip-4 659ms × (0.98,1.04) 660ms × (0.97,1.02) ~ (p=0.724)
BenchmarkGunzip-4 145ms × (0.98,1.04) 144ms × (0.99,1.04) ~ (p=0.671)
BenchmarkHTTPClientServer-4 139µs × (0.97,1.02) 138µs × (0.99,1.02) ~ (p=0.392)
BenchmarkJSONEncode-4 35.0ms × (0.99,1.02) 35.1ms × (0.98,1.02) ~ (p=0.777)
BenchmarkJSONDecode-4 119ms × (0.98,1.01) 118ms × (0.98,1.02) ~ (p=0.710)
BenchmarkMandelbrot200-4 6.02ms × (1.00,1.00) 6.02ms × (1.00,1.00) ~ (p=0.289)
BenchmarkGoParse-4 7.96ms × (0.99,1.01) 7.96ms × (0.99,1.01) ~ (p=0.884)
BenchmarkRegexpMatchEasy0_32-4 164ns × (0.98,1.04) 166ns × (0.97,1.04) ~ (p=0.221)
BenchmarkRegexpMatchEasy0_1K-4 540ns × (0.99,1.01) 552ns × (0.97,1.04) +2.10% (p=0.018)
BenchmarkRegexpMatchEasy1_32-4 140ns × (0.99,1.04) 142ns × (0.97,1.04) ~ (p=0.226)
BenchmarkRegexpMatchEasy1_1K-4 896ns × (0.99,1.03) 907ns × (0.97,1.04) ~ (p=0.155)
BenchmarkRegexpMatchMedium_32-4 255ns × (0.99,1.04) 255ns × (0.98,1.04) ~ (p=0.904)
BenchmarkRegexpMatchMedium_1K-4 73.4µs × (0.99,1.04) 73.8µs × (0.98,1.04) ~ (p=0.560)
BenchmarkRegexpMatchHard_32-4 3.93µs × (0.98,1.04) 3.95µs × (0.98,1.04) ~ (p=0.571)
BenchmarkRegexpMatchHard_1K-4 117µs × (1.00,1.01) 119µs × (0.98,1.04) +1.48% (p=0.048)
BenchmarkRevcomp-4 990ms × (0.94,1.08) 989ms × (0.94,1.10) ~ (p=0.957)
BenchmarkTemplate-4 137ms × (0.98,1.02) 137ms × (0.99,1.01) ~ (p=0.996)
BenchmarkTimeParse-4 629ns × (1.00,1.00) 629ns × (0.99,1.01) ~ (p=0.924)
BenchmarkTimeFormat-4 710ns × (0.99,1.01) 716ns × (0.98,1.02) +0.84% (p=0.033)
Change-Id: I43a04e0f6ad5e3ba9847dddf12e13222561f9cf4
Reviewed-on: https://go-review.googlesource.com/9543
Reviewed-by: Austin Clements <austin@google.com>
2015-04-30 00:17:09 -04:00
|
|
|
// Add assist utilization; avoid divide by zero.
|
2015-08-03 18:06:05 -04:00
|
|
|
if assistDuration > 0 {
|
|
|
|
|
utilization += float64(c.assistTime) / float64(assistDuration*int64(gomaxprocs))
|
2015-04-21 13:46:54 -04:00
|
|
|
}
|
runtime: schedule GC work more aggressively
Schedule the work as early as possible, while still respecting the
utilization percentage on average. The old code tried never to
go above the utilization percentage. The new code is willing
to go above the utilization percentage by one time slice
(but of course after doing that it must wait until the percentage
drops back down to the target before it gets another time slice).
The effect is that for concurrent GCs that can run in a small number
of time slices, the time during which write barriers are enabled is
reduced by one mutator + GC time slice round (possibly 30 ms per GC).
This only affects the fractional GC processor (the remainder of GOMAXPROCS/4),
so it matters most in GOMAXPROCS=1, a bit in GOMAXPROCS=2, and not at
all in GOMAXPROCS=4.
GOMAXPROCS=1
name old mean new mean delta
BenchmarkBinaryTree17 12.4s × (0.98,1.03) 13.5s × (0.97,1.04) +8.84% (p=0.000)
BenchmarkFannkuch11 4.38s × (1.00,1.01) 4.38s × (1.00,1.01) ~ (p=0.343)
BenchmarkFmtFprintfEmpty 88.9ns × (0.97,1.10) 90.1ns × (0.93,1.14) ~ (p=0.224)
BenchmarkFmtFprintfString 356ns × (0.94,1.05) 321ns × (0.94,1.12) -9.77% (p=0.000)
BenchmarkFmtFprintfInt 344ns × (0.98,1.03) 325ns × (0.96,1.03) -5.46% (p=0.000)
BenchmarkFmtFprintfIntInt 622ns × (0.97,1.03) 571ns × (0.95,1.05) -8.09% (p=0.000)
BenchmarkFmtFprintfPrefixedInt 462ns × (0.96,1.04) 431ns × (0.95,1.05) -6.81% (p=0.000)
BenchmarkFmtFprintfFloat 653ns × (0.98,1.03) 621ns × (0.99,1.03) -4.90% (p=0.000)
BenchmarkFmtManyArgs 2.32µs × (0.97,1.03) 2.19µs × (0.98,1.02) -5.43% (p=0.000)
BenchmarkGobDecode 27.0ms × (0.96,1.04) 20.0ms × (0.97,1.04) -26.06% (p=0.000)
BenchmarkGobEncode 26.6ms × (0.99,1.01) 17.8ms × (0.95,1.05) -33.19% (p=0.000)
BenchmarkGzip 659ms × (0.98,1.03) 650ms × (0.99,1.01) -1.34% (p=0.000)
BenchmarkGunzip 145ms × (0.98,1.04) 143ms × (1.00,1.01) -1.47% (p=0.000)
BenchmarkHTTPClientServer 111µs × (0.97,1.04) 110µs × (0.96,1.03) -1.30% (p=0.000)
BenchmarkJSONEncode 52.0ms × (0.97,1.03) 40.8ms × (0.97,1.03) -21.47% (p=0.000)
BenchmarkJSONDecode 127ms × (0.98,1.04) 120ms × (0.98,1.02) -5.55% (p=0.000)
BenchmarkMandelbrot200 6.04ms × (0.99,1.04) 6.02ms × (1.00,1.01) ~ (p=0.176)
BenchmarkGoParse 8.62ms × (0.96,1.08) 8.55ms × (0.93,1.09) ~ (p=0.302)
BenchmarkRegexpMatchEasy0_32 164ns × (0.98,1.05) 165ns × (0.98,1.07) ~ (p=0.293)
BenchmarkRegexpMatchEasy0_1K 546ns × (0.98,1.06) 547ns × (0.97,1.07) ~ (p=0.741)
BenchmarkRegexpMatchEasy1_32 142ns × (0.97,1.09) 141ns × (0.97,1.05) ~ (p=0.231)
BenchmarkRegexpMatchEasy1_1K 904ns × (0.97,1.07) 900ns × (0.98,1.04) ~ (p=0.294)
BenchmarkRegexpMatchMedium_32 256ns × (0.98,1.06) 256ns × (0.97,1.04) ~ (p=0.530)
BenchmarkRegexpMatchMedium_1K 74.2µs × (0.98,1.05) 73.8µs × (0.98,1.04) ~ (p=0.334)
BenchmarkRegexpMatchHard_32 3.94µs × (0.98,1.07) 3.92µs × (0.98,1.05) ~ (p=0.356)
BenchmarkRegexpMatchHard_1K 119µs × (0.98,1.07) 119µs × (0.98,1.06) ~ (p=0.467)
BenchmarkRevcomp 978ms × (0.96,1.09) 984ms × (0.95,1.07) ~ (p=0.448)
BenchmarkTemplate 151ms × (0.96,1.03) 142ms × (0.95,1.04) -5.55% (p=0.000)
BenchmarkTimeParse 628ns × (0.99,1.01) 628ns × (0.99,1.01) ~ (p=0.855)
BenchmarkTimeFormat 729ns × (0.98,1.06) 734ns × (0.97,1.05) ~ (p=0.149)
GOMAXPROCS=2
name old mean new mean delta
BenchmarkBinaryTree17-2 9.80s × (0.97,1.03) 9.85s × (0.99,1.02) ~ (p=0.444)
BenchmarkFannkuch11-2 4.35s × (0.99,1.01) 4.40s × (0.98,1.05) ~ (p=0.099)
BenchmarkFmtFprintfEmpty-2 86.7ns × (0.97,1.05) 85.9ns × (0.98,1.04) ~ (p=0.409)
BenchmarkFmtFprintfString-2 297ns × (0.98,1.01) 297ns × (0.99,1.01) ~ (p=0.743)
BenchmarkFmtFprintfInt-2 309ns × (0.98,1.02) 310ns × (0.99,1.01) ~ (p=0.464)
BenchmarkFmtFprintfIntInt-2 525ns × (0.97,1.05) 518ns × (0.99,1.01) ~ (p=0.151)
BenchmarkFmtFprintfPrefixedInt-2 408ns × (0.98,1.02) 408ns × (0.98,1.03) ~ (p=0.797)
BenchmarkFmtFprintfFloat-2 603ns × (0.99,1.01) 604ns × (0.98,1.02) ~ (p=0.588)
BenchmarkFmtManyArgs-2 2.07µs × (0.98,1.02) 2.05µs × (0.99,1.01) ~ (p=0.091)
BenchmarkGobDecode-2 19.1ms × (0.97,1.01) 19.3ms × (0.97,1.04) ~ (p=0.195)
BenchmarkGobEncode-2 16.2ms × (0.97,1.03) 16.4ms × (0.99,1.01) ~ (p=0.069)
BenchmarkGzip-2 652ms × (0.99,1.01) 651ms × (0.99,1.01) ~ (p=0.705)
BenchmarkGunzip-2 143ms × (1.00,1.01) 143ms × (1.00,1.00) ~ (p=0.665)
BenchmarkHTTPClientServer-2 149µs × (0.92,1.11) 149µs × (0.91,1.08) ~ (p=0.862)
BenchmarkJSONEncode-2 34.6ms × (0.98,1.02) 37.2ms × (0.99,1.01) +7.56% (p=0.000)
BenchmarkJSONDecode-2 117ms × (0.99,1.01) 117ms × (0.99,1.01) ~ (p=0.858)
BenchmarkMandelbrot200-2 6.10ms × (0.99,1.03) 6.03ms × (1.00,1.00) ~ (p=0.083)
BenchmarkGoParse-2 8.25ms × (0.98,1.01) 8.21ms × (0.99,1.02) ~ (p=0.307)
BenchmarkRegexpMatchEasy0_32-2 162ns × (0.99,1.02) 162ns × (0.99,1.01) ~ (p=0.857)
BenchmarkRegexpMatchEasy0_1K-2 541ns × (0.99,1.01) 540ns × (1.00,1.00) ~ (p=0.530)
BenchmarkRegexpMatchEasy1_32-2 138ns × (1.00,1.00) 141ns × (0.98,1.04) +1.88% (p=0.038)
BenchmarkRegexpMatchEasy1_1K-2 887ns × (0.99,1.01) 894ns × (0.99,1.01) ~ (p=0.087)
BenchmarkRegexpMatchMedium_32-2 252ns × (0.99,1.01) 252ns × (0.99,1.01) ~ (p=0.954)
BenchmarkRegexpMatchMedium_1K-2 73.4µs × (0.99,1.02) 72.8µs × (1.00,1.01) -0.87% (p=0.029)
BenchmarkRegexpMatchHard_32-2 3.95µs × (0.97,1.05) 3.87µs × (1.00,1.01) -2.11% (p=0.035)
BenchmarkRegexpMatchHard_1K-2 117µs × (0.99,1.01) 117µs × (0.99,1.01) ~ (p=0.669)
BenchmarkRevcomp-2 980ms × (0.95,1.03) 993ms × (0.94,1.09) ~ (p=0.527)
BenchmarkTemplate-2 136ms × (0.98,1.01) 135ms × (0.99,1.01) ~ (p=0.200)
BenchmarkTimeParse-2 630ns × (1.00,1.01) 630ns × (1.00,1.00) ~ (p=0.634)
BenchmarkTimeFormat-2 705ns × (0.99,1.01) 710ns × (0.98,1.02) ~ (p=0.174)
GOMAXPROCS=4
BenchmarkBinaryTree17-4 9.87s × (0.96,1.04) 9.75s × (0.96,1.03) ~ (p=0.178)
BenchmarkFannkuch11-4 4.35s × (1.00,1.01) 4.40s × (0.99,1.04) ~ (p=0.071)
BenchmarkFmtFprintfEmpty-4 85.8ns × (0.98,1.06) 85.6ns × (0.98,1.04) ~ (p=0.858)
BenchmarkFmtFprintfString-4 306ns × (0.99,1.03) 304ns × (0.97,1.02) ~ (p=0.470)
BenchmarkFmtFprintfInt-4 317ns × (0.98,1.01) 315ns × (0.98,1.02) -0.92% (p=0.044)
BenchmarkFmtFprintfIntInt-4 527ns × (0.99,1.01) 525ns × (0.98,1.01) ~ (p=0.164)
BenchmarkFmtFprintfPrefixedInt-4 421ns × (0.98,1.03) 417ns × (0.99,1.02) ~ (p=0.092)
BenchmarkFmtFprintfFloat-4 623ns × (0.98,1.02) 618ns × (0.98,1.03) ~ (p=0.172)
BenchmarkFmtManyArgs-4 2.09µs × (0.98,1.02) 2.09µs × (0.98,1.02) ~ (p=0.679)
BenchmarkGobDecode-4 18.6ms × (0.99,1.01) 18.6ms × (0.98,1.03) ~ (p=0.595)
BenchmarkGobEncode-4 15.0ms × (0.98,1.02) 15.1ms × (0.99,1.01) ~ (p=0.301)
BenchmarkGzip-4 659ms × (0.98,1.04) 660ms × (0.97,1.02) ~ (p=0.724)
BenchmarkGunzip-4 145ms × (0.98,1.04) 144ms × (0.99,1.04) ~ (p=0.671)
BenchmarkHTTPClientServer-4 139µs × (0.97,1.02) 138µs × (0.99,1.02) ~ (p=0.392)
BenchmarkJSONEncode-4 35.0ms × (0.99,1.02) 35.1ms × (0.98,1.02) ~ (p=0.777)
BenchmarkJSONDecode-4 119ms × (0.98,1.01) 118ms × (0.98,1.02) ~ (p=0.710)
BenchmarkMandelbrot200-4 6.02ms × (1.00,1.00) 6.02ms × (1.00,1.00) ~ (p=0.289)
BenchmarkGoParse-4 7.96ms × (0.99,1.01) 7.96ms × (0.99,1.01) ~ (p=0.884)
BenchmarkRegexpMatchEasy0_32-4 164ns × (0.98,1.04) 166ns × (0.97,1.04) ~ (p=0.221)
BenchmarkRegexpMatchEasy0_1K-4 540ns × (0.99,1.01) 552ns × (0.97,1.04) +2.10% (p=0.018)
BenchmarkRegexpMatchEasy1_32-4 140ns × (0.99,1.04) 142ns × (0.97,1.04) ~ (p=0.226)
BenchmarkRegexpMatchEasy1_1K-4 896ns × (0.99,1.03) 907ns × (0.97,1.04) ~ (p=0.155)
BenchmarkRegexpMatchMedium_32-4 255ns × (0.99,1.04) 255ns × (0.98,1.04) ~ (p=0.904)
BenchmarkRegexpMatchMedium_1K-4 73.4µs × (0.99,1.04) 73.8µs × (0.98,1.04) ~ (p=0.560)
BenchmarkRegexpMatchHard_32-4 3.93µs × (0.98,1.04) 3.95µs × (0.98,1.04) ~ (p=0.571)
BenchmarkRegexpMatchHard_1K-4 117µs × (1.00,1.01) 119µs × (0.98,1.04) +1.48% (p=0.048)
BenchmarkRevcomp-4 990ms × (0.94,1.08) 989ms × (0.94,1.10) ~ (p=0.957)
BenchmarkTemplate-4 137ms × (0.98,1.02) 137ms × (0.99,1.01) ~ (p=0.996)
BenchmarkTimeParse-4 629ns × (1.00,1.00) 629ns × (0.99,1.01) ~ (p=0.924)
BenchmarkTimeFormat-4 710ns × (0.99,1.01) 716ns × (0.98,1.02) +0.84% (p=0.033)
Change-Id: I43a04e0f6ad5e3ba9847dddf12e13222561f9cf4
Reviewed-on: https://go-review.googlesource.com/9543
Reviewed-by: Austin Clements <austin@google.com>
2015-04-30 00:17:09 -04:00
|
|
|
|
2017-03-31 17:09:41 -04:00
|
|
|
triggerError := goalGrowthRatio - memstats.triggerRatio - utilization/gcGoalUtilization*(actualGrowthRatio-memstats.triggerRatio)
|
2015-03-24 10:45:20 -04:00
|
|
|
|
|
|
|
|
// Finally, we adjust the trigger for next time by this error,
|
|
|
|
|
// damped by the proportional gain.
|
2017-03-31 17:09:41 -04:00
|
|
|
triggerRatio := memstats.triggerRatio + triggerGain*triggerError
|
2015-03-24 10:45:20 -04:00
|
|
|
|
2015-04-06 14:30:03 -04:00
|
|
|
if debug.gcpacertrace > 0 {
|
|
|
|
|
// Print controller state in terms of the design
|
|
|
|
|
// document.
|
|
|
|
|
H_m_prev := memstats.heap_marked
|
2017-03-31 17:09:41 -04:00
|
|
|
h_t := memstats.triggerRatio
|
2016-09-15 14:08:04 -04:00
|
|
|
H_T := memstats.gc_trigger
|
2015-04-06 14:30:03 -04:00
|
|
|
h_a := actualGrowthRatio
|
|
|
|
|
H_a := memstats.heap_live
|
|
|
|
|
h_g := goalGrowthRatio
|
|
|
|
|
H_g := int64(float64(H_m_prev) * (1 + h_g))
|
|
|
|
|
u_a := utilization
|
|
|
|
|
u_g := gcGoalUtilization
|
|
|
|
|
W_a := c.scanWork
|
|
|
|
|
print("pacer: H_m_prev=", H_m_prev,
|
|
|
|
|
" h_t=", h_t, " H_T=", H_T,
|
|
|
|
|
" h_a=", h_a, " H_a=", H_a,
|
|
|
|
|
" h_g=", h_g, " H_g=", H_g,
|
|
|
|
|
" u_a=", u_a, " u_g=", u_g,
|
runtime: use heap scan size as estimate of GC scan work
Currently, the GC uses a moving average of recent scan work ratios to
estimate the total scan work required by this cycle. This is in turn
used to compute how much scan work should be done by mutators when
they allocate in order to perform all expected scan work by the time
the allocated heap reaches the heap goal.
However, our current scan work estimate can be arbitrarily wrong if
the heap topography changes significantly from one cycle to the
next. For example, in the go1 benchmarks, at the beginning of each
benchmark, the heap is dominated by a 256MB no-scan object, so the GC
learns that the scan density of the heap is very low. In benchmarks
that then rapidly allocate pointer-dense objects, by the time of the
next GC cycle, our estimate of the scan work can be too low by a large
factor. This in turn lets the mutator allocate faster than the GC can
collect, allowing it to get arbitrarily far ahead of the scan work
estimate, which leads to very long GC cycles with very little mutator
assist that can overshoot the heap goal by large margins. This is
particularly easy to demonstrate with BinaryTree17:
$ GODEBUG=gctrace=1 ./go1.test -test.bench BinaryTree17
gc #1 @0.017s 2%: 0+0+0+0+0 ms clock, 0+0+0+0/0/0+0 ms cpu, 4->262->262 MB, 4 MB goal, 1 P
gc #2 @0.026s 3%: 0+0+0+0+0 ms clock, 0+0+0+0/0/0+0 ms cpu, 262->262->262 MB, 524 MB goal, 1 P
testing: warning: no tests to run
PASS
BenchmarkBinaryTree17 gc #3 @1.906s 0%: 0+0+0+0+7 ms clock, 0+0+0+0/0/0+7 ms cpu, 325->325->287 MB, 325 MB goal, 1 P (forced)
gc #4 @12.203s 20%: 0+0+0+10067+10 ms clock, 0+0+0+0/2523/852+10 ms cpu, 430->2092->1950 MB, 574 MB goal, 1 P
1 9150447353 ns/op
Change this estimate to instead use the *current* scannable heap
size. This has the advantage of being based solely on the current
state of the heap, not on past densities or reachable heap sizes, so
it isn't susceptible to falling behind during these sorts of phase
changes. This is strictly an over-estimate, but it's better to
over-estimate and get more assist than necessary than it is to
under-estimate and potentially spiral out of control. Experiments with
scaling this estimate back showed no obvious benefit for mutator
utilization, heap size, or assist time.
This new estimate has little effect for most benchmarks, including
most go1 benchmarks, x/benchmarks, and the 6g benchmark. It has a huge
effect for benchmarks that triggered the bad pacer behavior:
name old mean new mean delta
BinaryTree17 10.0s × (1.00,1.00) 3.5s × (0.98,1.01) -64.93% (p=0.000)
Fannkuch11 2.74s × (1.00,1.01) 2.65s × (1.00,1.00) -3.52% (p=0.000)
FmtFprintfEmpty 56.4ns × (0.99,1.00) 57.8ns × (1.00,1.01) +2.43% (p=0.000)
FmtFprintfString 187ns × (0.99,1.00) 185ns × (0.99,1.01) -1.19% (p=0.010)
FmtFprintfInt 184ns × (1.00,1.00) 183ns × (1.00,1.00) (no variance)
FmtFprintfIntInt 321ns × (1.00,1.00) 315ns × (1.00,1.00) -1.80% (p=0.000)
FmtFprintfPrefixedInt 266ns × (1.00,1.00) 263ns × (1.00,1.00) -1.22% (p=0.000)
FmtFprintfFloat 353ns × (1.00,1.00) 353ns × (1.00,1.00) -0.13% (p=0.035)
FmtManyArgs 1.21µs × (1.00,1.00) 1.19µs × (1.00,1.00) -1.33% (p=0.000)
GobDecode 9.69ms × (1.00,1.00) 9.59ms × (1.00,1.00) -1.07% (p=0.000)
GobEncode 7.89ms × (0.99,1.01) 7.74ms × (1.00,1.00) -1.92% (p=0.000)
Gzip 391ms × (1.00,1.00) 392ms × (1.00,1.00) ~ (p=0.522)
Gunzip 97.1ms × (1.00,1.00) 97.0ms × (1.00,1.00) -0.10% (p=0.000)
HTTPClientServer 55.7µs × (0.99,1.01) 56.7µs × (0.99,1.01) +1.81% (p=0.001)
JSONEncode 19.1ms × (1.00,1.00) 19.0ms × (1.00,1.00) -0.85% (p=0.000)
JSONDecode 66.8ms × (1.00,1.00) 66.9ms × (1.00,1.00) ~ (p=0.288)
Mandelbrot200 4.13ms × (1.00,1.00) 4.12ms × (1.00,1.00) -0.08% (p=0.000)
GoParse 3.97ms × (1.00,1.01) 4.01ms × (1.00,1.00) +0.99% (p=0.000)
RegexpMatchEasy0_32 114ns × (1.00,1.00) 115ns × (0.99,1.00) ~ (p=0.070)
RegexpMatchEasy0_1K 376ns × (1.00,1.00) 376ns × (1.00,1.00) ~ (p=0.900)
RegexpMatchEasy1_32 94.9ns × (1.00,1.00) 96.3ns × (1.00,1.01) +1.53% (p=0.001)
RegexpMatchEasy1_1K 568ns × (1.00,1.00) 567ns × (1.00,1.00) -0.22% (p=0.001)
RegexpMatchMedium_32 159ns × (1.00,1.00) 159ns × (1.00,1.00) ~ (p=0.178)
RegexpMatchMedium_1K 46.4µs × (1.00,1.00) 46.6µs × (1.00,1.00) +0.29% (p=0.000)
RegexpMatchHard_32 2.37µs × (1.00,1.00) 2.37µs × (1.00,1.00) ~ (p=0.722)
RegexpMatchHard_1K 71.1µs × (1.00,1.00) 71.2µs × (1.00,1.00) ~ (p=0.229)
Revcomp 565ms × (1.00,1.00) 562ms × (1.00,1.00) -0.52% (p=0.000)
Template 81.0ms × (1.00,1.00) 80.2ms × (1.00,1.00) -0.97% (p=0.000)
TimeParse 380ns × (1.00,1.00) 380ns × (1.00,1.00) ~ (p=0.148)
TimeFormat 405ns × (0.99,1.00) 385ns × (0.99,1.00) -5.00% (p=0.000)
Change-Id: I11274158bf3affaf62662e02de7af12d5fb789e4
Reviewed-on: https://go-review.googlesource.com/9696
Reviewed-by: Russ Cox <rsc@golang.org>
Run-TryBot: Austin Clements <austin@google.com>
2015-05-04 16:55:31 -04:00
|
|
|
" W_a=", W_a,
|
2015-04-06 14:30:03 -04:00
|
|
|
" goalΔ=", goalGrowthRatio-h_t,
|
|
|
|
|
" actualΔ=", h_a-h_t,
|
|
|
|
|
" u_a/u_g=", u_a/u_g,
|
|
|
|
|
"\n")
|
|
|
|
|
}
|
2017-03-31 17:09:41 -04:00
|
|
|
|
|
|
|
|
return triggerRatio
|
runtime: multi-threaded, utilization-scheduled background mark
Currently, the concurrent mark phase is performed by the main GC
goroutine. Prior to the previous commit enabling preemption, this
caused marking to always consume 1/GOMAXPROCS of the available CPU
time. If GOMAXPROCS=1, this meant background GC would consume 100% of
the CPU (effectively a STW). If GOMAXPROCS>4, background GC would use
less than the goal of 25%. If GOMAXPROCS=4, background GC would use
the goal 25%, but if the mutator wasn't using the remaining 75%,
background marking wouldn't take advantage of the idle time. Enabling
preemption in the previous commit made GC miss CPU targets in
completely different ways, but set us up to bring everything back in
line.
This change replaces the fixed GC goroutine with per-P background mark
goroutines. Once started, these goroutines don't go in the standard
run queues; instead, they are scheduled specially such that the time
spent in mutator assists and the background mark goroutines totals 25%
of the CPU time available to the program. Furthermore, this lets
background marking take advantage of idle Ps, which significantly
boosts GC performance for applications that under-utilize the CPU.
This requires also changing how time is reported for gctrace, so this
change splits the concurrent mark CPU time into assist/background/idle
scanning.
This also requires increasing the size of the StackRecord slice used
in a GoroutineProfile test.
Change-Id: I0936ff907d2cee6cb687a208f2df47e8988e3157
Reviewed-on: https://go-review.googlesource.com/8850
Reviewed-by: Rick Hudson <rlh@golang.org>
2015-03-23 21:07:33 -04:00
|
|
|
}
|
|
|
|
|
|
2015-10-26 17:07:02 -04:00
|
|
|
// enlistWorker encourages another dedicated mark worker to start on
|
|
|
|
|
// another P if there are spare worker slots. It is used by putfull
|
|
|
|
|
// when more work is made available.
|
|
|
|
|
//
|
|
|
|
|
//go:nowritebarrier
|
|
|
|
|
func (c *gcControllerState) enlistWorker() {
|
runtime: wake idle Ps when enqueuing GC work
If the scheduler has no user work and there's no GC work visible, it
puts the P to sleep (or blocks on the network). However, if we later
enqueue more GC work, there's currently nothing that specifically
wakes up the scheduler to let it start an idle GC worker. As a result,
we can underutilize the CPU during GC if Ps have been put to sleep.
Fix this by making GC wake idle Ps when work buffers are put on the
full list. We already have a hook to do this, since we use this to
preempt a random P if we need more dedicated workers. We expand this
hook to instead wake an idle P if there is one. The logic we use for
this is identical to the logic used to wake an idle P when we ready a
goroutine.
To make this really sound, we also fix the scheduler to re-check the
idle GC worker condition after releasing its P. This closes a race
where 1) the scheduler checks for idle work and finds none, 2) new
work is enqueued but there are no idle Ps so none are woken, and 3)
the scheduler releases its P.
There is one subtlety here. Currently we call enlistWorker directly
from putfull, but the gcWork is in an inconsistent state in the places
that call putfull. This isn't a problem right now because nothing that
enlistWorker does touches the gcWork, but with the added call to
wakep, it's possible to get a recursive call into the gcWork
(specifically, while write barriers are disallowed, this can do an
allocation, which can dispose a gcWork, which can put a workbuf). To
handle this, we lift the enlistWorker calls up a layer and delay them
until the gcWork is in a consistent state.
Fixes #14179.
Change-Id: Ia2467a52e54c9688c3c1752e1fc00f5b37bbfeeb
Reviewed-on: https://go-review.googlesource.com/32434
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Dmitry Vyukov <dvyukov@google.com>
2016-10-30 20:43:53 -04:00
|
|
|
// If there are idle Ps, wake one so it will run an idle worker.
|
runtime: do not call wakep from enlistWorker, to avoid possible deadlock
We have seen one instance of a production job suddenly spinning to
100% CPU and becoming unresponsive. In that one instance, a SIGQUIT
was sent after 328 minutes of spinning, and the stacks showed a single
goroutine in "IO wait (scan)" state.
Looking for things that might get stuck if a goroutine got stuck in
scanning a stack, we found that injectglist does:
lock(&sched.lock)
var n int
for n = 0; glist != nil; n++ {
gp := glist
glist = gp.schedlink.ptr()
casgstatus(gp, _Gwaiting, _Grunnable)
globrunqput(gp)
}
unlock(&sched.lock)
and that casgstatus spins on gp.atomicstatus until the _Gscan bit goes
away. Essentially, this code locks sched.lock and then while holding
sched.lock, waits to lock gp.atomicstatus.
The code that is doing the scan is:
if castogscanstatus(gp, s, s|_Gscan) {
if !gp.gcscandone {
scanstack(gp, gcw)
gp.gcscandone = true
}
restartg(gp)
break loop
}
More analysis showed that scanstack can, in a rare case, end up
calling back into code that acquires sched.lock. For example:
runtime.scanstack at proc.go:866
calls runtime.gentraceback at mgcmark.go:842
calls runtime.scanstack$1 at traceback.go:378
calls runtime.scanframeworker at mgcmark.go:819
calls runtime.scanblock at mgcmark.go:904
calls runtime.greyobject at mgcmark.go:1221
calls (*runtime.gcWork).put at mgcmark.go:1412
calls (*runtime.gcControllerState).enlistWorker at mgcwork.go:127
calls runtime.wakep at mgc.go:632
calls runtime.startm at proc.go:1779
acquires runtime.sched.lock at proc.go:1675
This path was found with an automated deadlock-detecting tool.
There are many such paths but they all go through enlistWorker -> wakep.
The evidence strongly suggests that one of these paths is what caused
the deadlock we observed. We're running those jobs with
GOTRACEBACK=crash now to try to get more information if it happens
again.
Further refinement and analysis shows that if we drop the wakep call
from enlistWorker, the remaining few deadlock cycles found by the tool
are all false positives caused by not understanding the effect of calls
to func variables.
The enlistWorker -> wakep call was intended only as a performance
optimization, it rarely executes, and if it does execute at just the
wrong time it can (and plausibly did) cause the deadlock we saw.
Comment it out, to avoid the potential deadlock.
Fixes #19112.
Unfixes #14179.
Change-Id: I6f7e10b890b991c11e79fab7aeefaf70b5d5a07b
Reviewed-on: https://go-review.googlesource.com/37093
Run-TryBot: Russ Cox <rsc@golang.org>
Reviewed-by: Austin Clements <austin@google.com>
2017-02-15 15:41:50 -05:00
|
|
|
// NOTE: This is suspected of causing deadlocks. See golang.org/issue/19112.
|
|
|
|
|
//
|
|
|
|
|
// if atomic.Load(&sched.npidle) != 0 && atomic.Load(&sched.nmspinning) == 0 {
|
|
|
|
|
// wakep()
|
|
|
|
|
// return
|
|
|
|
|
// }
|
runtime: wake idle Ps when enqueuing GC work
If the scheduler has no user work and there's no GC work visible, it
puts the P to sleep (or blocks on the network). However, if we later
enqueue more GC work, there's currently nothing that specifically
wakes up the scheduler to let it start an idle GC worker. As a result,
we can underutilize the CPU during GC if Ps have been put to sleep.
Fix this by making GC wake idle Ps when work buffers are put on the
full list. We already have a hook to do this, since we use this to
preempt a random P if we need more dedicated workers. We expand this
hook to instead wake an idle P if there is one. The logic we use for
this is identical to the logic used to wake an idle P when we ready a
goroutine.
To make this really sound, we also fix the scheduler to re-check the
idle GC worker condition after releasing its P. This closes a race
where 1) the scheduler checks for idle work and finds none, 2) new
work is enqueued but there are no idle Ps so none are woken, and 3)
the scheduler releases its P.
There is one subtlety here. Currently we call enlistWorker directly
from putfull, but the gcWork is in an inconsistent state in the places
that call putfull. This isn't a problem right now because nothing that
enlistWorker does touches the gcWork, but with the added call to
wakep, it's possible to get a recursive call into the gcWork
(specifically, while write barriers are disallowed, this can do an
allocation, which can dispose a gcWork, which can put a workbuf). To
handle this, we lift the enlistWorker calls up a layer and delay them
until the gcWork is in a consistent state.
Fixes #14179.
Change-Id: Ia2467a52e54c9688c3c1752e1fc00f5b37bbfeeb
Reviewed-on: https://go-review.googlesource.com/32434
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Dmitry Vyukov <dvyukov@google.com>
2016-10-30 20:43:53 -04:00
|
|
|
|
|
|
|
|
// There are no idle Ps. If we need more dedicated workers,
|
|
|
|
|
// try to preempt a running P so it will switch to a worker.
|
2015-10-26 17:07:02 -04:00
|
|
|
if c.dedicatedMarkWorkersNeeded <= 0 {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
// Pick a random other P to preempt.
|
|
|
|
|
if gomaxprocs <= 1 {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
gp := getg()
|
|
|
|
|
if gp == nil || gp.m == nil || gp.m.p == 0 {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
myID := gp.m.p.ptr().id
|
|
|
|
|
for tries := 0; tries < 5; tries++ {
|
2017-02-13 12:46:17 -08:00
|
|
|
id := int32(fastrandn(uint32(gomaxprocs - 1)))
|
2015-10-26 17:07:02 -04:00
|
|
|
if id >= myID {
|
|
|
|
|
id++
|
|
|
|
|
}
|
|
|
|
|
p := allp[id]
|
|
|
|
|
if p.status != _Prunning {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if preemptone(p) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-24 14:17:42 -04:00
|
|
|
// findRunnableGCWorker returns the background mark worker for _p_ if it
|
2015-03-27 17:01:53 -04:00
|
|
|
// should be run. This must only be called when gcBlackenEnabled != 0.
|
2015-04-24 14:17:42 -04:00
|
|
|
func (c *gcControllerState) findRunnableGCWorker(_p_ *p) *g {
|
2015-03-27 17:01:53 -04:00
|
|
|
if gcBlackenEnabled == 0 {
|
|
|
|
|
throw("gcControllerState.findRunnable: blackening not enabled")
|
runtime: multi-threaded, utilization-scheduled background mark
Currently, the concurrent mark phase is performed by the main GC
goroutine. Prior to the previous commit enabling preemption, this
caused marking to always consume 1/GOMAXPROCS of the available CPU
time. If GOMAXPROCS=1, this meant background GC would consume 100% of
the CPU (effectively a STW). If GOMAXPROCS>4, background GC would use
less than the goal of 25%. If GOMAXPROCS=4, background GC would use
the goal 25%, but if the mutator wasn't using the remaining 75%,
background marking wouldn't take advantage of the idle time. Enabling
preemption in the previous commit made GC miss CPU targets in
completely different ways, but set us up to bring everything back in
line.
This change replaces the fixed GC goroutine with per-P background mark
goroutines. Once started, these goroutines don't go in the standard
run queues; instead, they are scheduled specially such that the time
spent in mutator assists and the background mark goroutines totals 25%
of the CPU time available to the program. Furthermore, this lets
background marking take advantage of idle Ps, which significantly
boosts GC performance for applications that under-utilize the CPU.
This requires also changing how time is reported for gctrace, so this
change splits the concurrent mark CPU time into assist/background/idle
scanning.
This also requires increasing the size of the StackRecord slice used
in a GoroutineProfile test.
Change-Id: I0936ff907d2cee6cb687a208f2df47e8988e3157
Reviewed-on: https://go-review.googlesource.com/8850
Reviewed-by: Rick Hudson <rlh@golang.org>
2015-03-23 21:07:33 -04:00
|
|
|
}
|
2016-01-26 14:44:58 -05:00
|
|
|
if _p_.gcBgMarkWorker == 0 {
|
2015-10-26 11:27:37 -04:00
|
|
|
// The mark worker associated with this P is blocked
|
|
|
|
|
// performing a mark transition. We can't run it
|
|
|
|
|
// because it may be on some other run or wait queue.
|
runtime: multi-threaded, utilization-scheduled background mark
Currently, the concurrent mark phase is performed by the main GC
goroutine. Prior to the previous commit enabling preemption, this
caused marking to always consume 1/GOMAXPROCS of the available CPU
time. If GOMAXPROCS=1, this meant background GC would consume 100% of
the CPU (effectively a STW). If GOMAXPROCS>4, background GC would use
less than the goal of 25%. If GOMAXPROCS=4, background GC would use
the goal 25%, but if the mutator wasn't using the remaining 75%,
background marking wouldn't take advantage of the idle time. Enabling
preemption in the previous commit made GC miss CPU targets in
completely different ways, but set us up to bring everything back in
line.
This change replaces the fixed GC goroutine with per-P background mark
goroutines. Once started, these goroutines don't go in the standard
run queues; instead, they are scheduled specially such that the time
spent in mutator assists and the background mark goroutines totals 25%
of the CPU time available to the program. Furthermore, this lets
background marking take advantage of idle Ps, which significantly
boosts GC performance for applications that under-utilize the CPU.
This requires also changing how time is reported for gctrace, so this
change splits the concurrent mark CPU time into assist/background/idle
scanning.
This also requires increasing the size of the StackRecord slice used
in a GoroutineProfile test.
Change-Id: I0936ff907d2cee6cb687a208f2df47e8988e3157
Reviewed-on: https://go-review.googlesource.com/8850
Reviewed-by: Rick Hudson <rlh@golang.org>
2015-03-23 21:07:33 -04:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
runtime: eliminate getfull barrier from concurrent mark
Currently dedicated mark workers participate in the getfull barrier
during concurrent mark. However, the getfull barrier wasn't designed
for concurrent work and this causes no end of headaches.
In the concurrent setting, participants come and go. This makes mark
completion susceptible to live-lock: since dedicated workers are only
periodically polling for completion, it's possible for the program to
be in some transient worker each time one of the dedicated workers
wakes up to check if it can exit the getfull barrier. It also
complicates reasoning about the system because dedicated workers
participate directly in the getfull barrier, but transient workers
must instead use trygetfull because they have exit conditions that
aren't captured by getfull (e.g., fractional workers exit when
preempted). The complexity of implementing these exit conditions
contributed to #11677. Furthermore, the getfull barrier is inefficient
because we could be running user code instead of spinning on a P. In
effect, we're dedicating 25% of the CPU to marking even if that means
we have to spin to make that 25%. It also causes issues on Windows
because we can't actually sleep for 100µs (#8687).
Fix this by making dedicated workers no longer participate in the
getfull barrier. Instead, dedicated workers simply return to the
scheduler when they fail to get more work, regardless of what others
workers are doing, and the scheduler only starts new dedicated workers
if there's work available. Everything that needs to be handled by this
barrier is already handled by detection of mark completion.
This makes the system much more symmetric because all workers and
assists now use trygetfull during concurrent mark. It also loosens the
25% CPU target so that we can give some of that 25% back to user code
if there isn't enough work to keep the mark worker busy. And it
eliminates the problematic 100µs sleep on Windows during concurrent
mark (though not during mark termination).
The downside of this is that if we hit a bottleneck in the heap graph
that then expands back out, the system may shut down dedicated workers
and take a while to start them back up. We'll address this in the next
commit.
Updates #12041 and #8687.
No effect on the go1 benchmarks. This slows down the garbage benchmark
by 9%, but we'll more than make it up in the next commit.
name old time/op new time/op delta
XBenchGarbage-12 5.80ms ± 2% 6.32ms ± 4% +9.03% (p=0.000 n=20+20)
Change-Id: I65100a9ba005a8b5cf97940798918672ea9dd09b
Reviewed-on: https://go-review.googlesource.com/16297
Reviewed-by: Rick Hudson <rlh@golang.org>
2015-10-26 16:29:25 -04:00
|
|
|
if !gcMarkWorkAvailable(_p_) {
|
|
|
|
|
// No work to be done right now. This can happen at
|
|
|
|
|
// the end of the mark phase when there are still
|
|
|
|
|
// assists tapering off. Don't bother running a worker
|
|
|
|
|
// now because it'll just return immediately.
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-15 17:01:30 -04:00
|
|
|
decIfPositive := func(ptr *int64) bool {
|
|
|
|
|
if *ptr > 0 {
|
2015-11-02 14:09:24 -05:00
|
|
|
if atomic.Xaddint64(ptr, -1) >= 0 {
|
2015-04-15 17:01:30 -04:00
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
// We lost a race
|
2015-11-02 14:09:24 -05:00
|
|
|
atomic.Xaddint64(ptr, +1)
|
2015-04-15 17:01:30 -04:00
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if decIfPositive(&c.dedicatedMarkWorkersNeeded) {
|
|
|
|
|
// This P is now dedicated to marking until the end of
|
|
|
|
|
// the concurrent mark phase.
|
|
|
|
|
_p_.gcMarkWorkerMode = gcMarkWorkerDedicatedMode
|
2017-10-05 12:16:45 -04:00
|
|
|
} else if c.fractionalUtilizationGoal == 0 {
|
|
|
|
|
// No need for fractional workers.
|
|
|
|
|
return nil
|
2015-04-23 19:51:03 -04:00
|
|
|
} else {
|
2017-10-05 12:16:45 -04:00
|
|
|
// Is this P behind on the fractional utilization
|
|
|
|
|
// goal?
|
2017-10-04 16:15:35 -04:00
|
|
|
//
|
|
|
|
|
// This should be kept in sync with pollFractionalWorkerExit.
|
2017-10-05 12:16:45 -04:00
|
|
|
delta := nanotime() - gcController.markStartTime
|
|
|
|
|
if delta > 0 && float64(_p_.gcFractionalMarkTime)/float64(delta) > c.fractionalUtilizationGoal {
|
|
|
|
|
// Nope. No need to run a fractional worker.
|
2015-04-15 17:01:30 -04:00
|
|
|
return nil
|
runtime: multi-threaded, utilization-scheduled background mark
Currently, the concurrent mark phase is performed by the main GC
goroutine. Prior to the previous commit enabling preemption, this
caused marking to always consume 1/GOMAXPROCS of the available CPU
time. If GOMAXPROCS=1, this meant background GC would consume 100% of
the CPU (effectively a STW). If GOMAXPROCS>4, background GC would use
less than the goal of 25%. If GOMAXPROCS=4, background GC would use
the goal 25%, but if the mutator wasn't using the remaining 75%,
background marking wouldn't take advantage of the idle time. Enabling
preemption in the previous commit made GC miss CPU targets in
completely different ways, but set us up to bring everything back in
line.
This change replaces the fixed GC goroutine with per-P background mark
goroutines. Once started, these goroutines don't go in the standard
run queues; instead, they are scheduled specially such that the time
spent in mutator assists and the background mark goroutines totals 25%
of the CPU time available to the program. Furthermore, this lets
background marking take advantage of idle Ps, which significantly
boosts GC performance for applications that under-utilize the CPU.
This requires also changing how time is reported for gctrace, so this
change splits the concurrent mark CPU time into assist/background/idle
scanning.
This also requires increasing the size of the StackRecord slice used
in a GoroutineProfile test.
Change-Id: I0936ff907d2cee6cb687a208f2df47e8988e3157
Reviewed-on: https://go-review.googlesource.com/8850
Reviewed-by: Rick Hudson <rlh@golang.org>
2015-03-23 21:07:33 -04:00
|
|
|
}
|
2017-10-05 12:16:45 -04:00
|
|
|
// Run a fractional worker.
|
2015-04-15 17:01:30 -04:00
|
|
|
_p_.gcMarkWorkerMode = gcMarkWorkerFractionalMode
|
runtime: multi-threaded, utilization-scheduled background mark
Currently, the concurrent mark phase is performed by the main GC
goroutine. Prior to the previous commit enabling preemption, this
caused marking to always consume 1/GOMAXPROCS of the available CPU
time. If GOMAXPROCS=1, this meant background GC would consume 100% of
the CPU (effectively a STW). If GOMAXPROCS>4, background GC would use
less than the goal of 25%. If GOMAXPROCS=4, background GC would use
the goal 25%, but if the mutator wasn't using the remaining 75%,
background marking wouldn't take advantage of the idle time. Enabling
preemption in the previous commit made GC miss CPU targets in
completely different ways, but set us up to bring everything back in
line.
This change replaces the fixed GC goroutine with per-P background mark
goroutines. Once started, these goroutines don't go in the standard
run queues; instead, they are scheduled specially such that the time
spent in mutator assists and the background mark goroutines totals 25%
of the CPU time available to the program. Furthermore, this lets
background marking take advantage of idle Ps, which significantly
boosts GC performance for applications that under-utilize the CPU.
This requires also changing how time is reported for gctrace, so this
change splits the concurrent mark CPU time into assist/background/idle
scanning.
This also requires increasing the size of the StackRecord slice used
in a GoroutineProfile test.
Change-Id: I0936ff907d2cee6cb687a208f2df47e8988e3157
Reviewed-on: https://go-review.googlesource.com/8850
Reviewed-by: Rick Hudson <rlh@golang.org>
2015-03-23 21:07:33 -04:00
|
|
|
}
|
|
|
|
|
|
2015-04-15 17:01:30 -04:00
|
|
|
// Run the background mark worker
|
2016-01-26 14:44:58 -05:00
|
|
|
gp := _p_.gcBgMarkWorker.ptr()
|
2015-04-15 17:01:30 -04:00
|
|
|
casgstatus(gp, _Gwaiting, _Grunnable)
|
|
|
|
|
if trace.enabled {
|
|
|
|
|
traceGoUnpark(gp, 0)
|
|
|
|
|
}
|
|
|
|
|
return gp
|
2015-03-12 12:08:47 -04:00
|
|
|
}
|
|
|
|
|
|
2017-10-04 16:15:35 -04:00
|
|
|
// pollFractionalWorkerExit returns true if a fractional mark worker
|
|
|
|
|
// should self-preempt. It assumes it is called from the fractional
|
|
|
|
|
// worker.
|
|
|
|
|
func pollFractionalWorkerExit() bool {
|
|
|
|
|
// This should be kept in sync with the fractional worker
|
|
|
|
|
// scheduler logic in findRunnableGCWorker.
|
|
|
|
|
now := nanotime()
|
|
|
|
|
delta := now - gcController.markStartTime
|
|
|
|
|
if delta <= 0 {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
p := getg().m.p.ptr()
|
2017-10-05 12:16:45 -04:00
|
|
|
selfTime := p.gcFractionalMarkTime + (now - p.gcMarkWorkerStartTime)
|
2017-10-04 16:15:35 -04:00
|
|
|
// Add some slack to the utilization goal so that the
|
|
|
|
|
// fractional worker isn't behind again the instant it exits.
|
|
|
|
|
return float64(selfTime)/float64(delta) > 1.2*gcController.fractionalUtilizationGoal
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-03 12:10:56 -04:00
|
|
|
// gcSetTriggerRatio sets the trigger ratio and updates everything
|
2017-04-03 15:47:11 -04:00
|
|
|
// derived from it: the absolute trigger, the heap goal, mark pacing,
|
|
|
|
|
// and sweep pacing.
|
2017-04-03 12:10:56 -04:00
|
|
|
//
|
2017-04-03 15:47:11 -04:00
|
|
|
// This can be called any time. If GC is the in the middle of a
|
|
|
|
|
// concurrent phase, it will adjust the pacing of that phase.
|
2017-04-03 12:10:56 -04:00
|
|
|
//
|
|
|
|
|
// This depends on gcpercent, memstats.heap_marked, and
|
|
|
|
|
// memstats.heap_live. These must be up to date.
|
|
|
|
|
//
|
|
|
|
|
// mheap_.lock must be held or the world must be stopped.
|
|
|
|
|
func gcSetTriggerRatio(triggerRatio float64) {
|
|
|
|
|
// Set the trigger ratio, capped to reasonable bounds.
|
|
|
|
|
if triggerRatio < 0 {
|
|
|
|
|
// This can happen if the mutator is allocating very
|
|
|
|
|
// quickly or the GC is scanning very slowly.
|
|
|
|
|
triggerRatio = 0
|
|
|
|
|
} else if gcpercent >= 0 {
|
|
|
|
|
// Ensure there's always a little margin so that the
|
|
|
|
|
// mutator assist ratio isn't infinity.
|
|
|
|
|
maxTriggerRatio := 0.95 * float64(gcpercent) / 100
|
|
|
|
|
if triggerRatio > maxTriggerRatio {
|
|
|
|
|
triggerRatio = maxTriggerRatio
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
memstats.triggerRatio = triggerRatio
|
|
|
|
|
|
|
|
|
|
// Compute the absolute GC trigger from the trigger ratio.
|
|
|
|
|
//
|
|
|
|
|
// We trigger the next GC cycle when the allocated heap has
|
|
|
|
|
// grown by the trigger ratio over the marked heap size.
|
|
|
|
|
trigger := ^uint64(0)
|
|
|
|
|
if gcpercent >= 0 {
|
|
|
|
|
trigger = uint64(float64(memstats.heap_marked) * (1 + triggerRatio))
|
|
|
|
|
// Don't trigger below the minimum heap size.
|
|
|
|
|
minTrigger := heapminimum
|
|
|
|
|
if !gosweepdone() {
|
|
|
|
|
// Concurrent sweep happens in the heap growth
|
|
|
|
|
// from heap_live to gc_trigger, so ensure
|
|
|
|
|
// that concurrent sweep has some heap growth
|
|
|
|
|
// in which to perform sweeping before we
|
|
|
|
|
// start the next GC cycle.
|
|
|
|
|
sweepMin := atomic.Load64(&memstats.heap_live) + sweepMinHeapDistance*uint64(gcpercent)/100
|
|
|
|
|
if sweepMin > minTrigger {
|
|
|
|
|
minTrigger = sweepMin
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if trigger < minTrigger {
|
|
|
|
|
trigger = minTrigger
|
|
|
|
|
}
|
|
|
|
|
if int64(trigger) < 0 {
|
|
|
|
|
print("runtime: next_gc=", memstats.next_gc, " heap_marked=", memstats.heap_marked, " heap_live=", memstats.heap_live, " initialHeapLive=", work.initialHeapLive, "triggerRatio=", triggerRatio, " minTrigger=", minTrigger, "\n")
|
|
|
|
|
throw("gc_trigger underflow")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
memstats.gc_trigger = trigger
|
|
|
|
|
|
|
|
|
|
// Compute the next GC goal, which is when the allocated heap
|
|
|
|
|
// has grown by GOGC/100 over the heap marked by the last
|
|
|
|
|
// cycle.
|
|
|
|
|
goal := ^uint64(0)
|
|
|
|
|
if gcpercent >= 0 {
|
|
|
|
|
goal = memstats.heap_marked + memstats.heap_marked*uint64(gcpercent)/100
|
|
|
|
|
if goal < trigger {
|
|
|
|
|
// The trigger ratio is always less than GOGC/100, but
|
|
|
|
|
// other bounds on the trigger may have raised it.
|
|
|
|
|
// Push up the goal, too.
|
|
|
|
|
goal = trigger
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
memstats.next_gc = goal
|
|
|
|
|
if trace.enabled {
|
|
|
|
|
traceNextGC()
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-03 15:47:11 -04:00
|
|
|
// Update mark pacing.
|
|
|
|
|
if gcphase != _GCoff {
|
|
|
|
|
gcController.revise()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update sweep pacing.
|
2017-04-03 12:10:56 -04:00
|
|
|
if gosweepdone() {
|
|
|
|
|
mheap_.sweepPagesPerByte = 0
|
|
|
|
|
} else {
|
|
|
|
|
// Concurrent sweep needs to sweep all of the in-use
|
|
|
|
|
// pages by the time the allocated heap reaches the GC
|
|
|
|
|
// trigger. Compute the ratio of in-use pages to sweep
|
2017-04-03 15:47:11 -04:00
|
|
|
// per byte allocated, accounting for the fact that
|
|
|
|
|
// some might already be swept.
|
2017-04-03 15:22:06 -04:00
|
|
|
heapLiveBasis := atomic.Load64(&memstats.heap_live)
|
|
|
|
|
heapDistance := int64(trigger) - int64(heapLiveBasis)
|
2017-04-03 12:10:56 -04:00
|
|
|
// Add a little margin so rounding errors and
|
|
|
|
|
// concurrent sweep are less likely to leave pages
|
|
|
|
|
// unswept when GC starts.
|
|
|
|
|
heapDistance -= 1024 * 1024
|
|
|
|
|
if heapDistance < _PageSize {
|
|
|
|
|
// Avoid setting the sweep ratio extremely high
|
|
|
|
|
heapDistance = _PageSize
|
|
|
|
|
}
|
2017-04-03 15:47:11 -04:00
|
|
|
pagesSwept := atomic.Load64(&mheap_.pagesSwept)
|
|
|
|
|
sweepDistancePages := int64(mheap_.pagesInUse) - int64(pagesSwept)
|
|
|
|
|
if sweepDistancePages <= 0 {
|
|
|
|
|
mheap_.sweepPagesPerByte = 0
|
|
|
|
|
} else {
|
|
|
|
|
mheap_.sweepPagesPerByte = float64(sweepDistancePages) / float64(heapDistance)
|
|
|
|
|
mheap_.sweepHeapLiveBasis = heapLiveBasis
|
|
|
|
|
// Write pagesSweptBasis last, since this
|
|
|
|
|
// signals concurrent sweeps to recompute
|
|
|
|
|
// their debt.
|
|
|
|
|
atomic.Store64(&mheap_.pagesSweptBasis, pagesSwept)
|
|
|
|
|
}
|
2017-04-03 12:10:56 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-04 17:12:28 -04:00
|
|
|
// gcGoalUtilization is the goal CPU utilization for
|
2015-04-15 17:01:30 -04:00
|
|
|
// marking as a fraction of GOMAXPROCS.
|
runtime: multi-threaded, utilization-scheduled background mark
Currently, the concurrent mark phase is performed by the main GC
goroutine. Prior to the previous commit enabling preemption, this
caused marking to always consume 1/GOMAXPROCS of the available CPU
time. If GOMAXPROCS=1, this meant background GC would consume 100% of
the CPU (effectively a STW). If GOMAXPROCS>4, background GC would use
less than the goal of 25%. If GOMAXPROCS=4, background GC would use
the goal 25%, but if the mutator wasn't using the remaining 75%,
background marking wouldn't take advantage of the idle time. Enabling
preemption in the previous commit made GC miss CPU targets in
completely different ways, but set us up to bring everything back in
line.
This change replaces the fixed GC goroutine with per-P background mark
goroutines. Once started, these goroutines don't go in the standard
run queues; instead, they are scheduled specially such that the time
spent in mutator assists and the background mark goroutines totals 25%
of the CPU time available to the program. Furthermore, this lets
background marking take advantage of idle Ps, which significantly
boosts GC performance for applications that under-utilize the CPU.
This requires also changing how time is reported for gctrace, so this
change splits the concurrent mark CPU time into assist/background/idle
scanning.
This also requires increasing the size of the StackRecord slice used
in a GoroutineProfile test.
Change-Id: I0936ff907d2cee6cb687a208f2df47e8988e3157
Reviewed-on: https://go-review.googlesource.com/8850
Reviewed-by: Rick Hudson <rlh@golang.org>
2015-03-23 21:07:33 -04:00
|
|
|
const gcGoalUtilization = 0.25
|
|
|
|
|
|
2017-10-04 17:12:28 -04:00
|
|
|
// gcBackgroundUtilization is the fixed CPU utilization for background
|
|
|
|
|
// marking. It must be <= gcGoalUtilization. The difference between
|
|
|
|
|
// gcGoalUtilization and gcBackgroundUtilization will be made up by
|
2017-10-04 17:07:09 -04:00
|
|
|
// mark assists. The scheduler will aim to use within 50% of this
|
|
|
|
|
// goal.
|
2017-10-04 17:12:28 -04:00
|
|
|
const gcBackgroundUtilization = 0.25
|
|
|
|
|
|
2015-10-04 23:00:01 -04:00
|
|
|
// gcCreditSlack is the amount of scan work credit that can can
|
|
|
|
|
// accumulate locally before updating gcController.scanWork and,
|
|
|
|
|
// optionally, gcController.bgScanCredit. Lower values give a more
|
|
|
|
|
// accurate assist ratio and make it more likely that assists will
|
|
|
|
|
// successfully steal background credit. Higher values reduce memory
|
|
|
|
|
// contention.
|
|
|
|
|
const gcCreditSlack = 2000
|
2015-03-13 13:29:23 -04:00
|
|
|
|
2015-03-17 12:17:47 -04:00
|
|
|
// gcAssistTimeSlack is the nanoseconds of mutator assist time that
|
|
|
|
|
// can accumulate on a P before updating gcController.assistTime.
|
|
|
|
|
const gcAssistTimeSlack = 5000
|
|
|
|
|
|
2016-07-22 16:36:30 -07:00
|
|
|
// gcOverAssistWork determines how many extra units of scan work a GC
|
|
|
|
|
// assist does when an assist happens. This amortizes the cost of an
|
|
|
|
|
// assist by pre-paying for this many bytes of future allocations.
|
|
|
|
|
const gcOverAssistWork = 64 << 10
|
2015-10-04 20:56:11 -07:00
|
|
|
|
2015-02-19 15:48:40 -05:00
|
|
|
var work struct {
|
2017-03-07 16:38:29 -05:00
|
|
|
full lfstack // lock-free list of full blocks workbuf
|
|
|
|
|
empty lfstack // lock-free list of empty blocks workbuf
|
2015-11-11 12:39:30 -05:00
|
|
|
pad0 [sys.CacheLineSize]uint8 // prevents false-sharing between full/empty and nproc/nwait
|
runtime: perform concurrent scan in GC workers
Currently the concurrent root scan is performed in its entirety by the
GC coordinator before entering concurrent mark (which enables GC
workers). This scan is done sequentially, which can prolong the scan
phase, delay the mark phase, and means that the scan phase does not
obey the 25% CPU goal. Furthermore, there's no need to complete the
root scan before starting marking (in fact, we already allow GC
assists to happen during the scan phase), so this acts as an
unnecessary barrier between root scanning and marking.
This change shifts the root scan work out of the GC coordinator and in
to the GC workers. The coordinator simply sets up the scan state and
enqueues the right number of root scan jobs. The GC workers then drain
the root scan jobs prior to draining heap scan jobs.
This parallelizes the root scan process, makes it obey the 25% CPU
goal, and effectively eliminates root scanning as an isolated phase,
allowing the system to smoothly transition from root scanning to heap
marking. This also eliminates a major non-STW responsibility of the GC
coordinator, which will make it easier to switch to a decentralized
state machine. Finally, it puts us in a good position to perform root
scanning in assists as well, which will help satisfy assists at the
beginning of the GC cycle.
This is mostly straightforward. One tricky aspect is that we have to
deal with preemption deadlock: where two non-preemptible gorountines
are trying to preempt each other to perform a stack scan. Given the
context where this happens, the only instance of this is two
background workers trying to scan each other. We avoid this by simply
not scanning the stacks of background workers during the concurrent
phase; this is safe because we'll scan them during mark termination
(and their stacks are *very* small and should not contain any new
pointers).
This change also switches the root marking during mark termination to
use the same gcDrain-based code path as concurrent mark. This
shouldn't affect performance because STW root marking was already
parallel and tasks switched to heap marking immediately when no more
root marking tasks were available. However, it simplifies the code and
unifies these code paths.
This has negligible effect on the go1 benchmarks. It slightly slows
down the garbage benchmark, possibly by making GC run slightly more
frequently.
name old time/op new time/op delta
XBenchGarbage-12 5.10ms ± 1% 5.24ms ± 1% +2.87% (p=0.000 n=18+18)
name old time/op new time/op delta
BinaryTree17-12 3.25s ± 3% 3.20s ± 5% -1.57% (p=0.013 n=20+20)
Fannkuch11-12 2.45s ± 1% 2.46s ± 1% +0.38% (p=0.019 n=20+18)
FmtFprintfEmpty-12 49.7ns ± 3% 49.9ns ± 4% ~ (p=0.851 n=19+20)
FmtFprintfString-12 170ns ± 2% 170ns ± 1% ~ (p=0.775 n=20+19)
FmtFprintfInt-12 161ns ± 1% 160ns ± 1% -0.78% (p=0.000 n=19+18)
FmtFprintfIntInt-12 267ns ± 1% 270ns ± 1% +1.04% (p=0.000 n=19+19)
FmtFprintfPrefixedInt-12 238ns ± 2% 238ns ± 1% ~ (p=0.133 n=18+19)
FmtFprintfFloat-12 311ns ± 1% 310ns ± 2% -0.35% (p=0.023 n=20+19)
FmtManyArgs-12 1.08µs ± 1% 1.06µs ± 1% -2.31% (p=0.000 n=20+20)
GobDecode-12 8.65ms ± 1% 8.63ms ± 1% ~ (p=0.377 n=18+20)
GobEncode-12 6.49ms ± 1% 6.52ms ± 1% +0.37% (p=0.015 n=20+20)
Gzip-12 319ms ± 3% 318ms ± 1% ~ (p=0.975 n=19+17)
Gunzip-12 41.9ms ± 1% 42.1ms ± 2% +0.65% (p=0.004 n=19+20)
HTTPClientServer-12 61.7µs ± 1% 62.6µs ± 1% +1.40% (p=0.000 n=18+20)
JSONEncode-12 16.8ms ± 1% 16.9ms ± 1% ~ (p=0.239 n=20+18)
JSONDecode-12 58.4ms ± 1% 60.7ms ± 1% +3.85% (p=0.000 n=19+20)
Mandelbrot200-12 3.86ms ± 0% 3.86ms ± 1% ~ (p=0.092 n=18+19)
GoParse-12 3.75ms ± 2% 3.75ms ± 2% ~ (p=0.708 n=19+20)
RegexpMatchEasy0_32-12 100ns ± 1% 100ns ± 2% +0.60% (p=0.010 n=17+20)
RegexpMatchEasy0_1K-12 341ns ± 1% 342ns ± 2% ~ (p=0.203 n=20+19)
RegexpMatchEasy1_32-12 82.5ns ± 2% 83.2ns ± 2% +0.83% (p=0.007 n=19+19)
RegexpMatchEasy1_1K-12 495ns ± 1% 495ns ± 2% ~ (p=0.970 n=19+18)
RegexpMatchMedium_32-12 130ns ± 2% 130ns ± 2% +0.59% (p=0.039 n=19+20)
RegexpMatchMedium_1K-12 39.2µs ± 1% 39.3µs ± 1% ~ (p=0.214 n=18+18)
RegexpMatchHard_32-12 2.03µs ± 2% 2.02µs ± 1% ~ (p=0.166 n=18+19)
RegexpMatchHard_1K-12 61.0µs ± 1% 60.9µs ± 1% ~ (p=0.169 n=20+18)
Revcomp-12 533ms ± 1% 535ms ± 1% ~ (p=0.071 n=19+17)
Template-12 68.1ms ± 2% 73.0ms ± 1% +7.26% (p=0.000 n=19+20)
TimeParse-12 355ns ± 2% 356ns ± 2% ~ (p=0.530 n=19+20)
TimeFormat-12 357ns ± 2% 347ns ± 1% -2.59% (p=0.000 n=20+19)
[Geo mean] 62.1µs 62.3µs +0.31%
name old speed new speed delta
GobDecode-12 88.7MB/s ± 1% 88.9MB/s ± 1% ~ (p=0.377 n=18+20)
GobEncode-12 118MB/s ± 1% 118MB/s ± 1% -0.37% (p=0.015 n=20+20)
Gzip-12 60.9MB/s ± 3% 60.9MB/s ± 1% ~ (p=0.944 n=19+17)
Gunzip-12 464MB/s ± 1% 461MB/s ± 2% -0.64% (p=0.004 n=19+20)
JSONEncode-12 115MB/s ± 1% 115MB/s ± 1% ~ (p=0.236 n=20+18)
JSONDecode-12 33.2MB/s ± 1% 32.0MB/s ± 1% -3.71% (p=0.000 n=19+20)
GoParse-12 15.5MB/s ± 2% 15.5MB/s ± 2% ~ (p=0.702 n=19+20)
RegexpMatchEasy0_32-12 320MB/s ± 1% 318MB/s ± 2% ~ (p=0.094 n=18+20)
RegexpMatchEasy0_1K-12 3.00GB/s ± 1% 2.99GB/s ± 1% ~ (p=0.194 n=20+19)
RegexpMatchEasy1_32-12 388MB/s ± 2% 385MB/s ± 2% -0.83% (p=0.008 n=19+19)
RegexpMatchEasy1_1K-12 2.07GB/s ± 1% 2.07GB/s ± 1% ~ (p=0.964 n=19+18)
RegexpMatchMedium_32-12 7.68MB/s ± 1% 7.64MB/s ± 2% -0.57% (p=0.020 n=19+20)
RegexpMatchMedium_1K-12 26.1MB/s ± 1% 26.1MB/s ± 1% ~ (p=0.211 n=18+18)
RegexpMatchHard_32-12 15.8MB/s ± 1% 15.8MB/s ± 1% ~ (p=0.180 n=18+19)
RegexpMatchHard_1K-12 16.8MB/s ± 1% 16.8MB/s ± 2% ~ (p=0.236 n=20+19)
Revcomp-12 477MB/s ± 1% 475MB/s ± 1% ~ (p=0.071 n=19+17)
Template-12 28.5MB/s ± 2% 26.6MB/s ± 1% -6.77% (p=0.000 n=19+20)
[Geo mean] 100MB/s 99.0MB/s -0.82%
Change-Id: I875bf6ceb306d1ee2f470cabf88aa6ede27c47a0
Reviewed-on: https://go-review.googlesource.com/16059
Reviewed-by: Rick Hudson <rlh@golang.org>
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2015-10-19 13:46:32 -04:00
|
|
|
|
2017-03-20 14:05:48 -04:00
|
|
|
wbufSpans struct {
|
|
|
|
|
lock mutex
|
2017-03-20 17:25:59 -04:00
|
|
|
// free is a list of spans dedicated to workbufs, but
|
|
|
|
|
// that don't currently contain any workbufs.
|
|
|
|
|
free mSpanList
|
2017-03-20 14:05:48 -04:00
|
|
|
// busy is a list of all spans containing workbufs on
|
|
|
|
|
// one of the workbuf lists.
|
|
|
|
|
busy mSpanList
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Restore 64-bit alignment on 32-bit.
|
|
|
|
|
_ uint32
|
|
|
|
|
|
2016-11-14 18:24:37 -05:00
|
|
|
// bytesMarked is the number of bytes marked this cycle. This
|
|
|
|
|
// includes bytes blackened in scanned objects, noscan objects
|
|
|
|
|
// that go straight to black, and permagrey objects scanned by
|
|
|
|
|
// markroot during the concurrent scan phase. This is updated
|
|
|
|
|
// atomically during the cycle. Updates may be batched
|
|
|
|
|
// arbitrarily, since the value is only read at the end of the
|
|
|
|
|
// cycle.
|
|
|
|
|
//
|
|
|
|
|
// Because of benign races during marking, this number may not
|
|
|
|
|
// be the exact number of marked bytes, but it should be very
|
|
|
|
|
// close.
|
|
|
|
|
//
|
|
|
|
|
// Put this field here because it needs 64-bit atomic access
|
|
|
|
|
// (and thus 8-byte alignment even on 32-bit architectures).
|
|
|
|
|
bytesMarked uint64
|
|
|
|
|
|
runtime: perform concurrent scan in GC workers
Currently the concurrent root scan is performed in its entirety by the
GC coordinator before entering concurrent mark (which enables GC
workers). This scan is done sequentially, which can prolong the scan
phase, delay the mark phase, and means that the scan phase does not
obey the 25% CPU goal. Furthermore, there's no need to complete the
root scan before starting marking (in fact, we already allow GC
assists to happen during the scan phase), so this acts as an
unnecessary barrier between root scanning and marking.
This change shifts the root scan work out of the GC coordinator and in
to the GC workers. The coordinator simply sets up the scan state and
enqueues the right number of root scan jobs. The GC workers then drain
the root scan jobs prior to draining heap scan jobs.
This parallelizes the root scan process, makes it obey the 25% CPU
goal, and effectively eliminates root scanning as an isolated phase,
allowing the system to smoothly transition from root scanning to heap
marking. This also eliminates a major non-STW responsibility of the GC
coordinator, which will make it easier to switch to a decentralized
state machine. Finally, it puts us in a good position to perform root
scanning in assists as well, which will help satisfy assists at the
beginning of the GC cycle.
This is mostly straightforward. One tricky aspect is that we have to
deal with preemption deadlock: where two non-preemptible gorountines
are trying to preempt each other to perform a stack scan. Given the
context where this happens, the only instance of this is two
background workers trying to scan each other. We avoid this by simply
not scanning the stacks of background workers during the concurrent
phase; this is safe because we'll scan them during mark termination
(and their stacks are *very* small and should not contain any new
pointers).
This change also switches the root marking during mark termination to
use the same gcDrain-based code path as concurrent mark. This
shouldn't affect performance because STW root marking was already
parallel and tasks switched to heap marking immediately when no more
root marking tasks were available. However, it simplifies the code and
unifies these code paths.
This has negligible effect on the go1 benchmarks. It slightly slows
down the garbage benchmark, possibly by making GC run slightly more
frequently.
name old time/op new time/op delta
XBenchGarbage-12 5.10ms ± 1% 5.24ms ± 1% +2.87% (p=0.000 n=18+18)
name old time/op new time/op delta
BinaryTree17-12 3.25s ± 3% 3.20s ± 5% -1.57% (p=0.013 n=20+20)
Fannkuch11-12 2.45s ± 1% 2.46s ± 1% +0.38% (p=0.019 n=20+18)
FmtFprintfEmpty-12 49.7ns ± 3% 49.9ns ± 4% ~ (p=0.851 n=19+20)
FmtFprintfString-12 170ns ± 2% 170ns ± 1% ~ (p=0.775 n=20+19)
FmtFprintfInt-12 161ns ± 1% 160ns ± 1% -0.78% (p=0.000 n=19+18)
FmtFprintfIntInt-12 267ns ± 1% 270ns ± 1% +1.04% (p=0.000 n=19+19)
FmtFprintfPrefixedInt-12 238ns ± 2% 238ns ± 1% ~ (p=0.133 n=18+19)
FmtFprintfFloat-12 311ns ± 1% 310ns ± 2% -0.35% (p=0.023 n=20+19)
FmtManyArgs-12 1.08µs ± 1% 1.06µs ± 1% -2.31% (p=0.000 n=20+20)
GobDecode-12 8.65ms ± 1% 8.63ms ± 1% ~ (p=0.377 n=18+20)
GobEncode-12 6.49ms ± 1% 6.52ms ± 1% +0.37% (p=0.015 n=20+20)
Gzip-12 319ms ± 3% 318ms ± 1% ~ (p=0.975 n=19+17)
Gunzip-12 41.9ms ± 1% 42.1ms ± 2% +0.65% (p=0.004 n=19+20)
HTTPClientServer-12 61.7µs ± 1% 62.6µs ± 1% +1.40% (p=0.000 n=18+20)
JSONEncode-12 16.8ms ± 1% 16.9ms ± 1% ~ (p=0.239 n=20+18)
JSONDecode-12 58.4ms ± 1% 60.7ms ± 1% +3.85% (p=0.000 n=19+20)
Mandelbrot200-12 3.86ms ± 0% 3.86ms ± 1% ~ (p=0.092 n=18+19)
GoParse-12 3.75ms ± 2% 3.75ms ± 2% ~ (p=0.708 n=19+20)
RegexpMatchEasy0_32-12 100ns ± 1% 100ns ± 2% +0.60% (p=0.010 n=17+20)
RegexpMatchEasy0_1K-12 341ns ± 1% 342ns ± 2% ~ (p=0.203 n=20+19)
RegexpMatchEasy1_32-12 82.5ns ± 2% 83.2ns ± 2% +0.83% (p=0.007 n=19+19)
RegexpMatchEasy1_1K-12 495ns ± 1% 495ns ± 2% ~ (p=0.970 n=19+18)
RegexpMatchMedium_32-12 130ns ± 2% 130ns ± 2% +0.59% (p=0.039 n=19+20)
RegexpMatchMedium_1K-12 39.2µs ± 1% 39.3µs ± 1% ~ (p=0.214 n=18+18)
RegexpMatchHard_32-12 2.03µs ± 2% 2.02µs ± 1% ~ (p=0.166 n=18+19)
RegexpMatchHard_1K-12 61.0µs ± 1% 60.9µs ± 1% ~ (p=0.169 n=20+18)
Revcomp-12 533ms ± 1% 535ms ± 1% ~ (p=0.071 n=19+17)
Template-12 68.1ms ± 2% 73.0ms ± 1% +7.26% (p=0.000 n=19+20)
TimeParse-12 355ns ± 2% 356ns ± 2% ~ (p=0.530 n=19+20)
TimeFormat-12 357ns ± 2% 347ns ± 1% -2.59% (p=0.000 n=20+19)
[Geo mean] 62.1µs 62.3µs +0.31%
name old speed new speed delta
GobDecode-12 88.7MB/s ± 1% 88.9MB/s ± 1% ~ (p=0.377 n=18+20)
GobEncode-12 118MB/s ± 1% 118MB/s ± 1% -0.37% (p=0.015 n=20+20)
Gzip-12 60.9MB/s ± 3% 60.9MB/s ± 1% ~ (p=0.944 n=19+17)
Gunzip-12 464MB/s ± 1% 461MB/s ± 2% -0.64% (p=0.004 n=19+20)
JSONEncode-12 115MB/s ± 1% 115MB/s ± 1% ~ (p=0.236 n=20+18)
JSONDecode-12 33.2MB/s ± 1% 32.0MB/s ± 1% -3.71% (p=0.000 n=19+20)
GoParse-12 15.5MB/s ± 2% 15.5MB/s ± 2% ~ (p=0.702 n=19+20)
RegexpMatchEasy0_32-12 320MB/s ± 1% 318MB/s ± 2% ~ (p=0.094 n=18+20)
RegexpMatchEasy0_1K-12 3.00GB/s ± 1% 2.99GB/s ± 1% ~ (p=0.194 n=20+19)
RegexpMatchEasy1_32-12 388MB/s ± 2% 385MB/s ± 2% -0.83% (p=0.008 n=19+19)
RegexpMatchEasy1_1K-12 2.07GB/s ± 1% 2.07GB/s ± 1% ~ (p=0.964 n=19+18)
RegexpMatchMedium_32-12 7.68MB/s ± 1% 7.64MB/s ± 2% -0.57% (p=0.020 n=19+20)
RegexpMatchMedium_1K-12 26.1MB/s ± 1% 26.1MB/s ± 1% ~ (p=0.211 n=18+18)
RegexpMatchHard_32-12 15.8MB/s ± 1% 15.8MB/s ± 1% ~ (p=0.180 n=18+19)
RegexpMatchHard_1K-12 16.8MB/s ± 1% 16.8MB/s ± 2% ~ (p=0.236 n=20+19)
Revcomp-12 477MB/s ± 1% 475MB/s ± 1% ~ (p=0.071 n=19+17)
Template-12 28.5MB/s ± 2% 26.6MB/s ± 1% -6.77% (p=0.000 n=19+20)
[Geo mean] 100MB/s 99.0MB/s -0.82%
Change-Id: I875bf6ceb306d1ee2f470cabf88aa6ede27c47a0
Reviewed-on: https://go-review.googlesource.com/16059
Reviewed-by: Rick Hudson <rlh@golang.org>
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2015-10-19 13:46:32 -04:00
|
|
|
markrootNext uint32 // next markroot job
|
|
|
|
|
markrootJobs uint32 // number of markroot jobs
|
|
|
|
|
|
2015-02-19 13:38:46 -05:00
|
|
|
nproc uint32
|
|
|
|
|
tstart int64
|
|
|
|
|
nwait uint32
|
|
|
|
|
ndone uint32
|
|
|
|
|
alldone note
|
2014-11-15 08:00:38 -05:00
|
|
|
|
runtime: avoid getfull() barrier most of the time
With the hybrid barrier, unless we're doing a STW GC or hit a very
rare race (~once per all.bash) that can start mark termination before
all of the work is drained, we don't need to drain the work queue at
all. Even draining an empty work queue is rather expensive since we
have to enter the getfull() barrier, so it's worth avoiding this.
Conveniently, it's quite easy to detect whether or not we actually
need the getufull() barrier: since the world is stopped when we enter
mark termination, everything must have flushed its work to the work
queue, so we can just check the queue. If the queue is empty and we
haven't queued up any jobs that may create more work (which should
always be the case with the hybrid barrier), we can simply have all GC
workers perform non-blocking drains.
Also conveniently, this solution is quite safe. If we do somehow screw
something up and there's work on the work queue, some worker will
still process it, it just may not happen in parallel.
This is not the "right" solution, but it's simple, expedient,
low-risk, and maintains compatibility with debug.gcrescanstacks. When
we remove the gcrescanstacks fallback in Go 1.9, we should also fix
the race that starts mark termination early, and then we can eliminate
work draining from mark termination.
Updates #17503.
Change-Id: I7b3cd5de6a248ab29d78c2b42aed8b7443641361
Reviewed-on: https://go-review.googlesource.com/32186
Reviewed-by: Rick Hudson <rlh@golang.org>
2016-10-26 17:05:41 -04:00
|
|
|
// helperDrainBlock indicates that GC mark termination helpers
|
|
|
|
|
// should pass gcDrainBlock to gcDrain to block in the
|
|
|
|
|
// getfull() barrier. Otherwise, they should pass gcDrainNoBlock.
|
|
|
|
|
//
|
2017-02-09 11:50:26 -05:00
|
|
|
// TODO: This is a temporary fallback to work around races
|
|
|
|
|
// that cause early mark termination.
|
runtime: avoid getfull() barrier most of the time
With the hybrid barrier, unless we're doing a STW GC or hit a very
rare race (~once per all.bash) that can start mark termination before
all of the work is drained, we don't need to drain the work queue at
all. Even draining an empty work queue is rather expensive since we
have to enter the getfull() barrier, so it's worth avoiding this.
Conveniently, it's quite easy to detect whether or not we actually
need the getufull() barrier: since the world is stopped when we enter
mark termination, everything must have flushed its work to the work
queue, so we can just check the queue. If the queue is empty and we
haven't queued up any jobs that may create more work (which should
always be the case with the hybrid barrier), we can simply have all GC
workers perform non-blocking drains.
Also conveniently, this solution is quite safe. If we do somehow screw
something up and there's work on the work queue, some worker will
still process it, it just may not happen in parallel.
This is not the "right" solution, but it's simple, expedient,
low-risk, and maintains compatibility with debug.gcrescanstacks. When
we remove the gcrescanstacks fallback in Go 1.9, we should also fix
the race that starts mark termination early, and then we can eliminate
work draining from mark termination.
Updates #17503.
Change-Id: I7b3cd5de6a248ab29d78c2b42aed8b7443641361
Reviewed-on: https://go-review.googlesource.com/32186
Reviewed-by: Rick Hudson <rlh@golang.org>
2016-10-26 17:05:41 -04:00
|
|
|
helperDrainBlock bool
|
|
|
|
|
|
2015-10-16 16:52:26 -04:00
|
|
|
// Number of roots of various root types. Set by gcMarkRootPrepare.
|
2017-02-09 11:50:26 -05:00
|
|
|
nFlushCacheRoots int
|
|
|
|
|
nDataRoots, nBSSRoots, nSpanRoots, nStackRoots int
|
2015-10-16 16:52:26 -04:00
|
|
|
|
2016-02-15 18:24:06 -05:00
|
|
|
// markrootDone indicates that roots have been marked at least
|
|
|
|
|
// once during the current GC cycle. This is checked by root
|
|
|
|
|
// marking operations that have to happen only during the
|
|
|
|
|
// first root marking pass, whether that's during the
|
|
|
|
|
// concurrent mark phase in current GC or mark termination in
|
|
|
|
|
// STW GC.
|
|
|
|
|
markrootDone bool
|
runtime: scan objects with finalizers concurrently
This reduces pause time by ~25% relative to tip and by ~50% relative
to Go 1.5.1.
Currently one of the steps of STW mark termination is to loop (in
parallel) over all spans to find objects with finalizers in order to
mark all objects reachable from these objects and to treat the
finalizer special as a root. Unfortunately, even if there are no
finalizers at all, this loop takes roughly 1 ms/heap GB/core, so
multi-gigabyte heaps can quickly push our STW time past 10ms.
Fix this by moving this scan from mark termination to concurrent scan,
where it can run in parallel with mutators. The loop itself could also
be optimized, but this cost is small compared to concurrent marking.
Making this scan concurrent introduces two complications:
1) The scan currently walks the specials list of each span without
locking it, which is safe only with the world stopped. We fix this by
speculatively checking if a span has any specials (the vast majority
won't) and then locking the specials list only if there are specials
to check.
2) An object can have a finalizer set after concurrent scan, in which
case it won't have been marked appropriately by concurrent scan. If
the finalizer is a closure and is only reachable from the special, it
could be swept before it is run. Likewise, if the object is not marked
yet when the finalizer is set and then becomes unreachable before it
is marked, other objects reachable only from it may be swept before
the finalizer function is run. We fix this issue by making
addfinalizer ensure the same marking invariants as markroot does.
For multi-gigabyte heaps, this reduces max pause time by 20%–30%
relative to tip (depending on GOMAXPROCS) and by ~50% relative to Go
1.5.1 (where this loop was neither concurrent nor parallel). Here are
the results for the garbage benchmark:
---------------- max pause ----------------
Heap Procs Concurrent scan STW parallel scan 1.5.1
24GB 12 18ms 23ms 37ms
24GB 4 18ms 25ms 37ms
4GB 4 3.8ms 4.9ms 6.9ms
In all cases, 95%ile pause time is similar to the max pause time. This
also improves mean STW time by 10%–30%.
Fixes #11485.
Change-Id: I9359d8c3d120a51d23d924b52bf853a1299b1dfd
Reviewed-on: https://go-review.googlesource.com/14982
Reviewed-by: Rick Hudson <rlh@golang.org>
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2015-09-24 14:39:27 -04:00
|
|
|
|
2015-10-23 14:15:18 -04:00
|
|
|
// Each type of GC state transition is protected by a lock.
|
|
|
|
|
// Since multiple threads can simultaneously detect the state
|
|
|
|
|
// transition condition, any thread that detects a transition
|
|
|
|
|
// condition must acquire the appropriate transition lock,
|
|
|
|
|
// re-check the transition condition and return if it no
|
|
|
|
|
// longer holds or perform the transition if it does.
|
|
|
|
|
// Likewise, any transition must invalidate the transition
|
|
|
|
|
// condition before releasing the lock. This ensures that each
|
|
|
|
|
// transition is performed by exactly one thread and threads
|
|
|
|
|
// that need the transition to happen block until it has
|
|
|
|
|
// happened.
|
|
|
|
|
//
|
|
|
|
|
// startSema protects the transition from "off" to mark or
|
|
|
|
|
// mark termination.
|
|
|
|
|
startSema uint32
|
2015-10-26 11:27:37 -04:00
|
|
|
// markDoneSema protects transitions from mark 1 to mark 2 and
|
|
|
|
|
// from mark 2 to mark termination.
|
|
|
|
|
markDoneSema uint32
|
2015-10-23 14:15:18 -04:00
|
|
|
|
runtime: multi-threaded, utilization-scheduled background mark
Currently, the concurrent mark phase is performed by the main GC
goroutine. Prior to the previous commit enabling preemption, this
caused marking to always consume 1/GOMAXPROCS of the available CPU
time. If GOMAXPROCS=1, this meant background GC would consume 100% of
the CPU (effectively a STW). If GOMAXPROCS>4, background GC would use
less than the goal of 25%. If GOMAXPROCS=4, background GC would use
the goal 25%, but if the mutator wasn't using the remaining 75%,
background marking wouldn't take advantage of the idle time. Enabling
preemption in the previous commit made GC miss CPU targets in
completely different ways, but set us up to bring everything back in
line.
This change replaces the fixed GC goroutine with per-P background mark
goroutines. Once started, these goroutines don't go in the standard
run queues; instead, they are scheduled specially such that the time
spent in mutator assists and the background mark goroutines totals 25%
of the CPU time available to the program. Furthermore, this lets
background marking take advantage of idle Ps, which significantly
boosts GC performance for applications that under-utilize the CPU.
This requires also changing how time is reported for gctrace, so this
change splits the concurrent mark CPU time into assist/background/idle
scanning.
This also requires increasing the size of the StackRecord slice used
in a GoroutineProfile test.
Change-Id: I0936ff907d2cee6cb687a208f2df47e8988e3157
Reviewed-on: https://go-review.googlesource.com/8850
Reviewed-by: Rick Hudson <rlh@golang.org>
2015-03-23 21:07:33 -04:00
|
|
|
bgMarkReady note // signal background mark worker has started
|
|
|
|
|
bgMarkDone uint32 // cas to 1 when at a background mark completion point
|
2015-04-22 17:44:36 -04:00
|
|
|
// Background mark completion signaling
|
2015-06-01 18:16:03 -04:00
|
|
|
|
2015-10-23 15:17:04 -04:00
|
|
|
// mode is the concurrency mode of the current GC cycle.
|
|
|
|
|
mode gcMode
|
|
|
|
|
|
2017-02-27 10:46:12 -05:00
|
|
|
// userForced indicates the current GC cycle was forced by an
|
|
|
|
|
// explicit user call.
|
|
|
|
|
userForced bool
|
|
|
|
|
|
2015-04-01 13:47:35 -04:00
|
|
|
// totaltime is the CPU nanoseconds spent in GC since the
|
|
|
|
|
// program started if debug.gctrace > 0.
|
|
|
|
|
totaltime int64
|
2015-03-12 16:53:57 -04:00
|
|
|
|
runtime: fix underflow in next_gc calculation
Currently, it's possible for the next_gc calculation to underflow.
Since next_gc is unsigned, this wraps around and effectively disables
GC for the rest of the program's execution. Besides being obviously
wrong, this is causing test failures on 32-bit because some tests are
running out of heap.
This underflow happens for two reasons, both having to do with how we
estimate the reachable heap size at the end of the GC cycle.
One reason is that this calculation depends on the value of heap_live
at the beginning of the GC cycle, but we currently only record that
value during a concurrent GC and not during a forced STW GC. Fix this
by moving the recorded value from gcController to work and recording
it on a common code path.
The other reason is that we use the amount of allocation during the GC
cycle as an approximation of the amount of floating garbage and
subtract it from the marked heap to estimate the reachable heap.
However, since this is only an approximation, it's possible for the
amount of allocation during the cycle to be *larger* than the marked
heap size (since the runtime allocates white and it's possible for
these allocations to never be made reachable from the heap). Currently
this causes wrap-around in our estimate of the reachable heap size,
which in turn causes wrap-around in next_gc. Fix this by bottoming out
the reachable heap estimate at 0, in which case we just fall back to
triggering GC at heapminimum (which is okay since this only happens on
small heaps).
Fixes #10555, fixes #10556, and fixes #10559.
Change-Id: Iad07b529c03772356fede2ae557732f13ebfdb63
Reviewed-on: https://go-review.googlesource.com/9286
Run-TryBot: Austin Clements <austin@google.com>
Reviewed-by: Rick Hudson <rlh@golang.org>
2015-04-23 13:02:31 -04:00
|
|
|
// initialHeapLive is the value of memstats.heap_live at the
|
|
|
|
|
// beginning of this GC cycle.
|
|
|
|
|
initialHeapLive uint64
|
2015-10-14 21:31:33 -04:00
|
|
|
|
|
|
|
|
// assistQueue is a queue of assists that are blocked because
|
|
|
|
|
// there was neither enough credit to steal or enough work to
|
|
|
|
|
// do.
|
|
|
|
|
assistQueue struct {
|
|
|
|
|
lock mutex
|
|
|
|
|
head, tail guintptr
|
|
|
|
|
}
|
2015-10-23 15:17:04 -04:00
|
|
|
|
2017-02-23 21:50:19 -05:00
|
|
|
// sweepWaiters is a list of blocked goroutines to wake when
|
|
|
|
|
// we transition from mark termination to sweep.
|
|
|
|
|
sweepWaiters struct {
|
|
|
|
|
lock mutex
|
|
|
|
|
head guintptr
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// cycles is the number of completed GC cycles, where a GC
|
|
|
|
|
// cycle is sweep termination, mark, mark termination, and
|
|
|
|
|
// sweep. This differs from memstats.numgc, which is
|
|
|
|
|
// incremented at mark termination.
|
|
|
|
|
cycles uint32
|
|
|
|
|
|
2015-10-23 15:17:04 -04:00
|
|
|
// Timing/utilization stats for this cycle.
|
|
|
|
|
stwprocs, maxprocs int32
|
|
|
|
|
tSweepTerm, tMark, tMarkTerm, tEnd int64 // nanotime() of phase start
|
|
|
|
|
|
|
|
|
|
pauseNS int64 // total STW time this cycle
|
|
|
|
|
pauseStart int64 // nanotime() of last STW
|
|
|
|
|
|
|
|
|
|
// debug.gctrace heap sizes for this cycle.
|
|
|
|
|
heap0, heap1, heap2, heapGoal uint64
|
2015-01-06 14:58:49 -05:00
|
|
|
}
|
|
|
|
|
|
2015-07-18 23:22:18 -07:00
|
|
|
// GC runs a garbage collection and blocks the caller until the
|
|
|
|
|
// garbage collection is complete. It may also block the entire
|
|
|
|
|
// program.
|
2015-02-19 13:38:46 -05:00
|
|
|
func GC() {
|
2017-02-23 21:50:19 -05:00
|
|
|
// We consider a cycle to be: sweep termination, mark, mark
|
|
|
|
|
// termination, and sweep. This function shouldn't return
|
|
|
|
|
// until a full cycle has been completed, from beginning to
|
|
|
|
|
// end. Hence, we always want to finish up the current cycle
|
|
|
|
|
// and start a new one. That means:
|
|
|
|
|
//
|
|
|
|
|
// 1. In sweep termination, mark, or mark termination of cycle
|
|
|
|
|
// N, wait until mark termination N completes and transitions
|
|
|
|
|
// to sweep N.
|
|
|
|
|
//
|
|
|
|
|
// 2. In sweep N, help with sweep N.
|
|
|
|
|
//
|
|
|
|
|
// At this point we can begin a full cycle N+1.
|
|
|
|
|
//
|
|
|
|
|
// 3. Trigger cycle N+1 by starting sweep termination N+1.
|
|
|
|
|
//
|
|
|
|
|
// 4. Wait for mark termination N+1 to complete.
|
|
|
|
|
//
|
|
|
|
|
// 5. Help with sweep N+1 until it's done.
|
|
|
|
|
//
|
|
|
|
|
// This all has to be written to deal with the fact that the
|
|
|
|
|
// GC may move ahead on its own. For example, when we block
|
|
|
|
|
// until mark termination N, we may wake up in cycle N+2.
|
|
|
|
|
|
|
|
|
|
gp := getg()
|
|
|
|
|
|
|
|
|
|
// Prevent the GC phase or cycle count from changing.
|
|
|
|
|
lock(&work.sweepWaiters.lock)
|
|
|
|
|
n := atomic.Load(&work.cycles)
|
|
|
|
|
if gcphase == _GCmark {
|
|
|
|
|
// Wait until sweep termination, mark, and mark
|
|
|
|
|
// termination of cycle N complete.
|
|
|
|
|
gp.schedlink = work.sweepWaiters.head
|
|
|
|
|
work.sweepWaiters.head.set(gp)
|
|
|
|
|
goparkunlock(&work.sweepWaiters.lock, "wait for GC cycle", traceEvGoBlock, 1)
|
|
|
|
|
} else {
|
|
|
|
|
// We're in sweep N already.
|
|
|
|
|
unlock(&work.sweepWaiters.lock)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// We're now in sweep N or later. Trigger GC cycle N+1, which
|
|
|
|
|
// will first finish sweep N if necessary and then enter sweep
|
|
|
|
|
// termination N+1.
|
|
|
|
|
gcStart(gcBackgroundMode, gcTrigger{kind: gcTriggerCycle, n: n + 1})
|
|
|
|
|
|
|
|
|
|
// Wait for mark termination N+1 to complete.
|
|
|
|
|
lock(&work.sweepWaiters.lock)
|
|
|
|
|
if gcphase == _GCmark && atomic.Load(&work.cycles) == n+1 {
|
|
|
|
|
gp.schedlink = work.sweepWaiters.head
|
|
|
|
|
work.sweepWaiters.head.set(gp)
|
|
|
|
|
goparkunlock(&work.sweepWaiters.lock, "wait for GC cycle", traceEvGoBlock, 1)
|
|
|
|
|
} else {
|
|
|
|
|
unlock(&work.sweepWaiters.lock)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Finish sweep N+1 before returning. We do this both to
|
|
|
|
|
// complete the cycle and because runtime.GC() is often used
|
|
|
|
|
// as part of tests and benchmarks to get the system into a
|
|
|
|
|
// relatively stable and isolated state.
|
|
|
|
|
for atomic.Load(&work.cycles) == n+1 && gosweepone() != ^uintptr(0) {
|
|
|
|
|
sweep.nbgsweep++
|
|
|
|
|
Gosched()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Callers may assume that the heap profile reflects the
|
|
|
|
|
// just-completed cycle when this returns (historically this
|
|
|
|
|
// happened because this was a STW GC), but right now the
|
|
|
|
|
// profile still reflects mark termination N, not N+1.
|
|
|
|
|
//
|
|
|
|
|
// As soon as all of the sweep frees from cycle N+1 are done,
|
|
|
|
|
// we can go ahead and publish the heap profile.
|
|
|
|
|
//
|
|
|
|
|
// First, wait for sweeping to finish. (We know there are no
|
|
|
|
|
// more spans on the sweep queue, but we may be concurrently
|
|
|
|
|
// sweeping spans, so we have to wait.)
|
|
|
|
|
for atomic.Load(&work.cycles) == n+1 && atomic.Load(&mheap_.sweepers) != 0 {
|
|
|
|
|
Gosched()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Now we're really done with sweeping, so we can publish the
|
|
|
|
|
// stable heap profile. Only do this if we haven't already hit
|
|
|
|
|
// another mark termination.
|
|
|
|
|
mp := acquirem()
|
|
|
|
|
cycle := atomic.Load(&work.cycles)
|
|
|
|
|
if cycle == n+1 || (gcphase == _GCmark && cycle == n+2) {
|
|
|
|
|
mProf_PostSweep()
|
|
|
|
|
}
|
|
|
|
|
releasem(mp)
|
2014-11-11 17:05:02 -05:00
|
|
|
}
|
|
|
|
|
|
2015-09-24 14:30:09 -04:00
|
|
|
// gcMode indicates how concurrent a GC cycle should be.
|
|
|
|
|
type gcMode int
|
|
|
|
|
|
2015-02-19 15:48:40 -05:00
|
|
|
const (
|
2015-09-24 14:30:09 -04:00
|
|
|
gcBackgroundMode gcMode = iota // concurrent GC and sweep
|
|
|
|
|
gcForceMode // stop-the-world GC now, concurrent sweep
|
2016-12-06 17:42:42 -05:00
|
|
|
gcForceBlockMode // stop-the-world GC now and STW sweep (forced by user)
|
2015-02-19 15:48:40 -05:00
|
|
|
)
|
|
|
|
|
|
2017-01-09 11:35:42 -05:00
|
|
|
// A gcTrigger is a predicate for starting a GC cycle. Specifically,
|
|
|
|
|
// it is an exit condition for the _GCoff phase.
|
|
|
|
|
type gcTrigger struct {
|
|
|
|
|
kind gcTriggerKind
|
2017-02-23 21:50:19 -05:00
|
|
|
now int64 // gcTriggerTime: current time
|
|
|
|
|
n uint32 // gcTriggerCycle: cycle number to start
|
2017-01-09 11:35:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type gcTriggerKind int
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
// gcTriggerAlways indicates that a cycle should be started
|
2017-02-23 11:54:43 -05:00
|
|
|
// unconditionally, even if GOGC is off or we're in a cycle
|
|
|
|
|
// right now. This cannot be consolidated with other cycles.
|
2017-01-09 11:35:42 -05:00
|
|
|
gcTriggerAlways gcTriggerKind = iota
|
|
|
|
|
|
|
|
|
|
// gcTriggerHeap indicates that a cycle should be started when
|
|
|
|
|
// the heap size reaches the trigger heap size computed by the
|
|
|
|
|
// controller.
|
|
|
|
|
gcTriggerHeap
|
|
|
|
|
|
|
|
|
|
// gcTriggerTime indicates that a cycle should be started when
|
|
|
|
|
// it's been more than forcegcperiod nanoseconds since the
|
|
|
|
|
// previous GC cycle.
|
|
|
|
|
gcTriggerTime
|
2017-02-23 21:50:19 -05:00
|
|
|
|
|
|
|
|
// gcTriggerCycle indicates that a cycle should be started if
|
|
|
|
|
// we have not yet started cycle number gcTrigger.n (relative
|
|
|
|
|
// to work.cycles).
|
|
|
|
|
gcTriggerCycle
|
2017-01-09 11:35:42 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// test returns true if the trigger condition is satisfied, meaning
|
|
|
|
|
// that the exit condition for the _GCoff phase has been met. The exit
|
|
|
|
|
// condition should be tested when allocating.
|
|
|
|
|
func (t gcTrigger) test() bool {
|
2017-02-23 11:54:43 -05:00
|
|
|
if !memstats.enablegc || panicking != 0 {
|
2017-01-09 11:35:42 -05:00
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
if t.kind == gcTriggerAlways {
|
|
|
|
|
return true
|
|
|
|
|
}
|
2017-09-25 14:58:13 -04:00
|
|
|
if gcphase != _GCoff {
|
2017-01-09 11:35:42 -05:00
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
switch t.kind {
|
|
|
|
|
case gcTriggerHeap:
|
2017-04-21 11:45:44 -04:00
|
|
|
// Non-atomic access to heap_live for performance. If
|
|
|
|
|
// we are going to trigger on this, this thread just
|
|
|
|
|
// atomically wrote heap_live anyway and we'll see our
|
|
|
|
|
// own write.
|
2017-01-09 11:35:42 -05:00
|
|
|
return memstats.heap_live >= memstats.gc_trigger
|
|
|
|
|
case gcTriggerTime:
|
2017-09-25 14:58:13 -04:00
|
|
|
if gcpercent < 0 {
|
|
|
|
|
return false
|
|
|
|
|
}
|
2017-01-09 11:35:42 -05:00
|
|
|
lastgc := int64(atomic.Load64(&memstats.last_gc_nanotime))
|
|
|
|
|
return lastgc != 0 && t.now-lastgc > forcegcperiod
|
2017-02-23 21:50:19 -05:00
|
|
|
case gcTriggerCycle:
|
|
|
|
|
// t.n > work.cycles, but accounting for wraparound.
|
|
|
|
|
return int32(t.n-work.cycles) > 0
|
2017-01-09 11:35:42 -05:00
|
|
|
}
|
|
|
|
|
return true
|
2015-10-23 14:15:18 -04:00
|
|
|
}
|
|
|
|
|
|
2017-01-09 11:35:42 -05:00
|
|
|
// gcStart transitions the GC from _GCoff to _GCmark (if
|
|
|
|
|
// !mode.stwMark) or _GCmarktermination (if mode.stwMark) by
|
|
|
|
|
// performing sweep termination and GC initialization.
|
2015-10-23 14:15:18 -04:00
|
|
|
//
|
|
|
|
|
// This may return without performing this transition in some cases,
|
|
|
|
|
// such as when called on a system stack or with locks held.
|
2017-01-09 11:35:42 -05:00
|
|
|
func gcStart(mode gcMode, trigger gcTrigger) {
|
2015-10-23 14:15:18 -04:00
|
|
|
// Since this is called from malloc and malloc is called in
|
|
|
|
|
// the guts of a number of libraries that might be holding
|
|
|
|
|
// locks, don't attempt to start GC in non-preemptible or
|
|
|
|
|
// potentially unstable situations.
|
|
|
|
|
mp := acquirem()
|
|
|
|
|
if gp := getg(); gp == mp.g0 || mp.locks > 1 || mp.preemptoff != "" {
|
|
|
|
|
releasem(mp)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
releasem(mp)
|
|
|
|
|
mp = nil
|
|
|
|
|
|
2015-10-23 15:04:37 -04:00
|
|
|
// Pick up the remaining unswept/not being swept spans concurrently
|
|
|
|
|
//
|
|
|
|
|
// This shouldn't happen if we're being invoked in background
|
|
|
|
|
// mode since proportional sweep should have just finished
|
|
|
|
|
// sweeping everything, but rounding errors, etc, may leave a
|
|
|
|
|
// few spans unswept. In forced mode, this is necessary since
|
|
|
|
|
// GC can be forced at any point in the sweeping cycle.
|
|
|
|
|
//
|
|
|
|
|
// We check the transition condition continuously here in case
|
|
|
|
|
// this G gets delayed in to the next GC cycle.
|
2017-01-09 11:35:42 -05:00
|
|
|
for trigger.test() && gosweepone() != ^uintptr(0) {
|
2015-10-23 15:04:37 -04:00
|
|
|
sweep.nbgsweep++
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-23 14:15:18 -04:00
|
|
|
// Perform GC initialization and the sweep termination
|
|
|
|
|
// transition.
|
2017-02-23 11:54:43 -05:00
|
|
|
semacquire(&work.startSema)
|
|
|
|
|
// Re-check transition condition under transition lock.
|
|
|
|
|
if !trigger.test() {
|
|
|
|
|
semrelease(&work.startSema)
|
|
|
|
|
return
|
2015-10-23 14:15:18 -04:00
|
|
|
}
|
|
|
|
|
|
2016-12-06 17:42:42 -05:00
|
|
|
// For stats, check if this GC was forced by the user.
|
2017-02-23 21:50:19 -05:00
|
|
|
work.userForced = trigger.kind == gcTriggerAlways || trigger.kind == gcTriggerCycle
|
2016-12-06 17:42:42 -05:00
|
|
|
|
2015-10-23 14:15:18 -04:00
|
|
|
// In gcstoptheworld debug mode, upgrade the mode accordingly.
|
|
|
|
|
// We do this after re-checking the transition condition so
|
|
|
|
|
// that multiple goroutines that detect the heap trigger don't
|
|
|
|
|
// start multiple STW GCs.
|
|
|
|
|
if mode == gcBackgroundMode {
|
|
|
|
|
if debug.gcstoptheworld == 1 {
|
|
|
|
|
mode = gcForceMode
|
|
|
|
|
} else if debug.gcstoptheworld == 2 {
|
|
|
|
|
mode = gcForceBlockMode
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-19 22:33:51 +02:00
|
|
|
// Ok, we're doing it! Stop everybody else
|
2016-12-13 16:45:55 +01:00
|
|
|
semacquire(&worldsema)
|
2014-12-12 18:41:57 +01:00
|
|
|
|
2015-07-01 11:04:19 -04:00
|
|
|
if trace.enabled {
|
|
|
|
|
traceGCStart()
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-19 15:48:40 -05:00
|
|
|
if mode == gcBackgroundMode {
|
runtime: multi-threaded, utilization-scheduled background mark
Currently, the concurrent mark phase is performed by the main GC
goroutine. Prior to the previous commit enabling preemption, this
caused marking to always consume 1/GOMAXPROCS of the available CPU
time. If GOMAXPROCS=1, this meant background GC would consume 100% of
the CPU (effectively a STW). If GOMAXPROCS>4, background GC would use
less than the goal of 25%. If GOMAXPROCS=4, background GC would use
the goal 25%, but if the mutator wasn't using the remaining 75%,
background marking wouldn't take advantage of the idle time. Enabling
preemption in the previous commit made GC miss CPU targets in
completely different ways, but set us up to bring everything back in
line.
This change replaces the fixed GC goroutine with per-P background mark
goroutines. Once started, these goroutines don't go in the standard
run queues; instead, they are scheduled specially such that the time
spent in mutator assists and the background mark goroutines totals 25%
of the CPU time available to the program. Furthermore, this lets
background marking take advantage of idle Ps, which significantly
boosts GC performance for applications that under-utilize the CPU.
This requires also changing how time is reported for gctrace, so this
change splits the concurrent mark CPU time into assist/background/idle
scanning.
This also requires increasing the size of the StackRecord slice used
in a GoroutineProfile test.
Change-Id: I0936ff907d2cee6cb687a208f2df47e8988e3157
Reviewed-on: https://go-review.googlesource.com/8850
Reviewed-by: Rick Hudson <rlh@golang.org>
2015-03-23 21:07:33 -04:00
|
|
|
gcBgMarkStartWorkers()
|
2014-11-11 17:05:02 -05:00
|
|
|
}
|
2016-03-01 15:09:24 -05:00
|
|
|
|
|
|
|
|
gcResetMarkState()
|
|
|
|
|
|
2015-10-23 15:17:04 -04:00
|
|
|
work.stwprocs, work.maxprocs = gcprocs(), gomaxprocs
|
2017-04-21 11:45:44 -04:00
|
|
|
work.heap0 = atomic.Load64(&memstats.heap_live)
|
2015-10-23 15:17:04 -04:00
|
|
|
work.pauseNS = 0
|
|
|
|
|
work.mode = mode
|
|
|
|
|
|
2017-07-18 11:21:15 -04:00
|
|
|
now := nanotime()
|
|
|
|
|
work.tSweepTerm = now
|
2015-10-23 15:17:04 -04:00
|
|
|
work.pauseStart = now
|
2017-07-21 14:25:28 -04:00
|
|
|
if trace.enabled {
|
|
|
|
|
traceGCSTWStart(1)
|
|
|
|
|
}
|
2015-05-15 16:00:50 -04:00
|
|
|
systemstack(stopTheWorldWithSema)
|
runtime: remove sweep wait loop in finishsweep_m
In general, finishsweep_m must block until any spans that are
concurrently being swept have been swept. It accomplishes this by
looping over all spans, which, as in the previous commit, takes
~1ms/heap GB. Unfortunately, we do this during the STW sweep
termination phase, so multi-gigabyte heaps can push our STW time past
10ms.
However, there's no need to do this wait if the world is stopped
because, in effect, stopping the world already had to wait for
anything that was sweeping (and if it didn't, the wait in
finishsweep_m would deadlock). Hence, we can simply skip this loop if
the world is stopped, such as during sweep termination. In fact,
currently all calls to finishsweep_m are STW, but this hasn't always
been the case and may not be the case in the future, so we keep the
logic around.
For 24GB heaps, this reduces max pause time by 75% relative to tip and
by 90% relative to Go 1.5. Notably, all pauses are now well under
10ms. Here are the results for the garbage benchmark:
------------- max pause ------------
Heap Procs after change before change 1.5.1
24GB 12 3.8ms 16ms 37ms
24GB 4 3.7ms 16ms 37ms
4GB 4 3.7ms 3ms 6.9ms
In the 4GB/4P case, it seems the "before change" run got lucky: the
max went up, but the 99%ile pause time went down from 3ms to 2.04ms.
Change-Id: Ica22189559f231d408ef2815019c9dbb5f38bf31
Reviewed-on: https://go-review.googlesource.com/15071
Reviewed-by: Rick Hudson <rlh@golang.org>
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2015-09-26 14:00:57 -04:00
|
|
|
// Finish sweep before we start concurrent scan.
|
|
|
|
|
systemstack(func() {
|
2016-10-05 21:22:33 -04:00
|
|
|
finishsweep_m()
|
runtime: remove sweep wait loop in finishsweep_m
In general, finishsweep_m must block until any spans that are
concurrently being swept have been swept. It accomplishes this by
looping over all spans, which, as in the previous commit, takes
~1ms/heap GB. Unfortunately, we do this during the STW sweep
termination phase, so multi-gigabyte heaps can push our STW time past
10ms.
However, there's no need to do this wait if the world is stopped
because, in effect, stopping the world already had to wait for
anything that was sweeping (and if it didn't, the wait in
finishsweep_m would deadlock). Hence, we can simply skip this loop if
the world is stopped, such as during sweep termination. In fact,
currently all calls to finishsweep_m are STW, but this hasn't always
been the case and may not be the case in the future, so we keep the
logic around.
For 24GB heaps, this reduces max pause time by 75% relative to tip and
by 90% relative to Go 1.5. Notably, all pauses are now well under
10ms. Here are the results for the garbage benchmark:
------------- max pause ------------
Heap Procs after change before change 1.5.1
24GB 12 3.8ms 16ms 37ms
24GB 4 3.7ms 16ms 37ms
4GB 4 3.7ms 3ms 6.9ms
In the 4GB/4P case, it seems the "before change" run got lucky: the
max went up, but the 99%ile pause time went down from 3ms to 2.04ms.
Change-Id: Ica22189559f231d408ef2815019c9dbb5f38bf31
Reviewed-on: https://go-review.googlesource.com/15071
Reviewed-by: Rick Hudson <rlh@golang.org>
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2015-09-26 14:00:57 -04:00
|
|
|
})
|
2015-03-05 17:33:08 -05:00
|
|
|
// clearpools before we start the GC. If we wait they memory will not be
|
|
|
|
|
// reclaimed until the next GC cycle.
|
|
|
|
|
clearpools()
|
2015-02-19 15:48:40 -05:00
|
|
|
|
2017-02-23 21:50:19 -05:00
|
|
|
work.cycles++
|
2015-02-19 15:48:40 -05:00
|
|
|
if mode == gcBackgroundMode { // Do as much work concurrently as possible
|
2015-03-12 12:08:47 -04:00
|
|
|
gcController.startCycle()
|
2016-09-15 14:08:04 -04:00
|
|
|
work.heapGoal = memstats.next_gc
|
2015-03-12 12:08:47 -04:00
|
|
|
|
2015-10-24 21:19:52 -04:00
|
|
|
// Enter concurrent mark phase and enable
|
|
|
|
|
// write barriers.
|
|
|
|
|
//
|
|
|
|
|
// Because the world is stopped, all Ps will
|
|
|
|
|
// observe that write barriers are enabled by
|
|
|
|
|
// the time we start the world and begin
|
|
|
|
|
// scanning.
|
|
|
|
|
//
|
2017-02-09 14:03:49 -05:00
|
|
|
// Write barriers must be enabled before assists are
|
2015-10-24 21:19:52 -04:00
|
|
|
// enabled because they must be enabled before
|
|
|
|
|
// any non-leaf heap objects are marked. Since
|
|
|
|
|
// allocations are blocked until assists can
|
|
|
|
|
// happen, we want enable assists as early as
|
|
|
|
|
// possible.
|
|
|
|
|
setGCPhase(_GCmark)
|
|
|
|
|
|
|
|
|
|
gcBgMarkPrepare() // Must happen before assist enable.
|
|
|
|
|
gcMarkRootPrepare()
|
|
|
|
|
|
2016-09-09 09:34:26 -04:00
|
|
|
// Mark all active tinyalloc blocks. Since we're
|
|
|
|
|
// allocating from these, they need to be black like
|
|
|
|
|
// other allocations. The alternative is to blacken
|
|
|
|
|
// the tiny block on every allocation from it, which
|
|
|
|
|
// would slow down the tiny allocator.
|
|
|
|
|
gcMarkTinyAllocs()
|
|
|
|
|
|
2015-10-24 21:19:52 -04:00
|
|
|
// At this point all Ps have enabled the write
|
|
|
|
|
// barrier, thus maintaining the no white to
|
|
|
|
|
// black invariant. Enable mutator assists to
|
|
|
|
|
// put back-pressure on fast allocating
|
|
|
|
|
// mutators.
|
2015-11-02 14:09:24 -05:00
|
|
|
atomic.Store(&gcBlackenEnabled, 1)
|
2015-10-24 21:19:52 -04:00
|
|
|
|
2015-10-26 11:27:37 -04:00
|
|
|
// Assists and workers can start the moment we start
|
|
|
|
|
// the world.
|
2016-03-02 17:27:59 -05:00
|
|
|
gcController.markStartTime = now
|
2015-10-26 11:27:37 -04:00
|
|
|
|
2015-10-24 21:19:52 -04:00
|
|
|
// Concurrent mark.
|
2017-07-24 16:06:10 -04:00
|
|
|
systemstack(func() {
|
2017-07-21 14:25:28 -04:00
|
|
|
now = startTheWorldWithSema(trace.enabled)
|
2017-07-24 16:06:10 -04:00
|
|
|
})
|
2015-10-24 21:19:52 -04:00
|
|
|
work.pauseNS += now - work.pauseStart
|
2015-10-23 15:17:04 -04:00
|
|
|
work.tMark = now
|
2015-10-23 15:55:03 -04:00
|
|
|
} else {
|
2017-07-21 14:25:28 -04:00
|
|
|
if trace.enabled {
|
|
|
|
|
// Switch to mark termination STW.
|
|
|
|
|
traceGCSTWDone()
|
|
|
|
|
traceGCSTWStart(0)
|
|
|
|
|
}
|
2015-10-23 15:55:03 -04:00
|
|
|
t := nanotime()
|
|
|
|
|
work.tMark, work.tMarkTerm = t, t
|
|
|
|
|
work.heapGoal = work.heap0
|
|
|
|
|
|
|
|
|
|
// Perform mark termination. This will restart the world.
|
2017-03-31 17:09:41 -04:00
|
|
|
gcMarkTermination(memstats.triggerRatio)
|
2015-10-23 15:55:03 -04:00
|
|
|
}
|
|
|
|
|
|
2017-02-23 11:54:43 -05:00
|
|
|
semrelease(&work.startSema)
|
2015-10-23 15:55:03 -04:00
|
|
|
}
|
|
|
|
|
|
2015-10-24 21:30:59 -04:00
|
|
|
// gcMarkDone transitions the GC from mark 1 to mark 2 and from mark 2
|
|
|
|
|
// to mark termination.
|
|
|
|
|
//
|
|
|
|
|
// This should be called when all mark work has been drained. In mark
|
|
|
|
|
// 1, this includes all root marking jobs, global work buffers, and
|
|
|
|
|
// active work buffers in assists and background workers; however,
|
|
|
|
|
// work may still be cached in per-P work buffers. In mark 2, per-P
|
|
|
|
|
// caches are disabled.
|
2015-11-23 11:37:12 -05:00
|
|
|
//
|
|
|
|
|
// The calling context must be preemptible.
|
|
|
|
|
//
|
|
|
|
|
// Note that it is explicitly okay to have write barriers in this
|
|
|
|
|
// function because completion of concurrent mark is best-effort
|
|
|
|
|
// anyway. Any work created by write barriers here will be cleaned up
|
|
|
|
|
// by mark termination.
|
2015-10-24 21:30:59 -04:00
|
|
|
func gcMarkDone() {
|
2015-11-23 11:37:12 -05:00
|
|
|
top:
|
2016-12-13 16:45:55 +01:00
|
|
|
semacquire(&work.markDoneSema)
|
2015-10-24 21:30:59 -04:00
|
|
|
|
2015-10-26 11:27:37 -04:00
|
|
|
// Re-check transition condition under transition lock.
|
|
|
|
|
if !(gcphase == _GCmark && work.nwait == work.nproc && !gcMarkWorkAvailable(nil)) {
|
|
|
|
|
semrelease(&work.markDoneSema)
|
|
|
|
|
return
|
|
|
|
|
}
|
2015-06-01 18:16:03 -04:00
|
|
|
|
2015-10-26 11:27:37 -04:00
|
|
|
// Disallow starting new workers so that any remaining workers
|
|
|
|
|
// in the current mark phase will drain out.
|
|
|
|
|
//
|
|
|
|
|
// TODO(austin): Should dedicated workers keep an eye on this
|
|
|
|
|
// and exit gcDrain promptly?
|
2015-11-02 14:09:24 -05:00
|
|
|
atomic.Xaddint64(&gcController.dedicatedMarkWorkersNeeded, -0xffffffff)
|
2017-10-05 12:16:45 -04:00
|
|
|
prevFractionalGoal := gcController.fractionalUtilizationGoal
|
|
|
|
|
gcController.fractionalUtilizationGoal = 0
|
runtime: perform concurrent scan in GC workers
Currently the concurrent root scan is performed in its entirety by the
GC coordinator before entering concurrent mark (which enables GC
workers). This scan is done sequentially, which can prolong the scan
phase, delay the mark phase, and means that the scan phase does not
obey the 25% CPU goal. Furthermore, there's no need to complete the
root scan before starting marking (in fact, we already allow GC
assists to happen during the scan phase), so this acts as an
unnecessary barrier between root scanning and marking.
This change shifts the root scan work out of the GC coordinator and in
to the GC workers. The coordinator simply sets up the scan state and
enqueues the right number of root scan jobs. The GC workers then drain
the root scan jobs prior to draining heap scan jobs.
This parallelizes the root scan process, makes it obey the 25% CPU
goal, and effectively eliminates root scanning as an isolated phase,
allowing the system to smoothly transition from root scanning to heap
marking. This also eliminates a major non-STW responsibility of the GC
coordinator, which will make it easier to switch to a decentralized
state machine. Finally, it puts us in a good position to perform root
scanning in assists as well, which will help satisfy assists at the
beginning of the GC cycle.
This is mostly straightforward. One tricky aspect is that we have to
deal with preemption deadlock: where two non-preemptible gorountines
are trying to preempt each other to perform a stack scan. Given the
context where this happens, the only instance of this is two
background workers trying to scan each other. We avoid this by simply
not scanning the stacks of background workers during the concurrent
phase; this is safe because we'll scan them during mark termination
(and their stacks are *very* small and should not contain any new
pointers).
This change also switches the root marking during mark termination to
use the same gcDrain-based code path as concurrent mark. This
shouldn't affect performance because STW root marking was already
parallel and tasks switched to heap marking immediately when no more
root marking tasks were available. However, it simplifies the code and
unifies these code paths.
This has negligible effect on the go1 benchmarks. It slightly slows
down the garbage benchmark, possibly by making GC run slightly more
frequently.
name old time/op new time/op delta
XBenchGarbage-12 5.10ms ± 1% 5.24ms ± 1% +2.87% (p=0.000 n=18+18)
name old time/op new time/op delta
BinaryTree17-12 3.25s ± 3% 3.20s ± 5% -1.57% (p=0.013 n=20+20)
Fannkuch11-12 2.45s ± 1% 2.46s ± 1% +0.38% (p=0.019 n=20+18)
FmtFprintfEmpty-12 49.7ns ± 3% 49.9ns ± 4% ~ (p=0.851 n=19+20)
FmtFprintfString-12 170ns ± 2% 170ns ± 1% ~ (p=0.775 n=20+19)
FmtFprintfInt-12 161ns ± 1% 160ns ± 1% -0.78% (p=0.000 n=19+18)
FmtFprintfIntInt-12 267ns ± 1% 270ns ± 1% +1.04% (p=0.000 n=19+19)
FmtFprintfPrefixedInt-12 238ns ± 2% 238ns ± 1% ~ (p=0.133 n=18+19)
FmtFprintfFloat-12 311ns ± 1% 310ns ± 2% -0.35% (p=0.023 n=20+19)
FmtManyArgs-12 1.08µs ± 1% 1.06µs ± 1% -2.31% (p=0.000 n=20+20)
GobDecode-12 8.65ms ± 1% 8.63ms ± 1% ~ (p=0.377 n=18+20)
GobEncode-12 6.49ms ± 1% 6.52ms ± 1% +0.37% (p=0.015 n=20+20)
Gzip-12 319ms ± 3% 318ms ± 1% ~ (p=0.975 n=19+17)
Gunzip-12 41.9ms ± 1% 42.1ms ± 2% +0.65% (p=0.004 n=19+20)
HTTPClientServer-12 61.7µs ± 1% 62.6µs ± 1% +1.40% (p=0.000 n=18+20)
JSONEncode-12 16.8ms ± 1% 16.9ms ± 1% ~ (p=0.239 n=20+18)
JSONDecode-12 58.4ms ± 1% 60.7ms ± 1% +3.85% (p=0.000 n=19+20)
Mandelbrot200-12 3.86ms ± 0% 3.86ms ± 1% ~ (p=0.092 n=18+19)
GoParse-12 3.75ms ± 2% 3.75ms ± 2% ~ (p=0.708 n=19+20)
RegexpMatchEasy0_32-12 100ns ± 1% 100ns ± 2% +0.60% (p=0.010 n=17+20)
RegexpMatchEasy0_1K-12 341ns ± 1% 342ns ± 2% ~ (p=0.203 n=20+19)
RegexpMatchEasy1_32-12 82.5ns ± 2% 83.2ns ± 2% +0.83% (p=0.007 n=19+19)
RegexpMatchEasy1_1K-12 495ns ± 1% 495ns ± 2% ~ (p=0.970 n=19+18)
RegexpMatchMedium_32-12 130ns ± 2% 130ns ± 2% +0.59% (p=0.039 n=19+20)
RegexpMatchMedium_1K-12 39.2µs ± 1% 39.3µs ± 1% ~ (p=0.214 n=18+18)
RegexpMatchHard_32-12 2.03µs ± 2% 2.02µs ± 1% ~ (p=0.166 n=18+19)
RegexpMatchHard_1K-12 61.0µs ± 1% 60.9µs ± 1% ~ (p=0.169 n=20+18)
Revcomp-12 533ms ± 1% 535ms ± 1% ~ (p=0.071 n=19+17)
Template-12 68.1ms ± 2% 73.0ms ± 1% +7.26% (p=0.000 n=19+20)
TimeParse-12 355ns ± 2% 356ns ± 2% ~ (p=0.530 n=19+20)
TimeFormat-12 357ns ± 2% 347ns ± 1% -2.59% (p=0.000 n=20+19)
[Geo mean] 62.1µs 62.3µs +0.31%
name old speed new speed delta
GobDecode-12 88.7MB/s ± 1% 88.9MB/s ± 1% ~ (p=0.377 n=18+20)
GobEncode-12 118MB/s ± 1% 118MB/s ± 1% -0.37% (p=0.015 n=20+20)
Gzip-12 60.9MB/s ± 3% 60.9MB/s ± 1% ~ (p=0.944 n=19+17)
Gunzip-12 464MB/s ± 1% 461MB/s ± 2% -0.64% (p=0.004 n=19+20)
JSONEncode-12 115MB/s ± 1% 115MB/s ± 1% ~ (p=0.236 n=20+18)
JSONDecode-12 33.2MB/s ± 1% 32.0MB/s ± 1% -3.71% (p=0.000 n=19+20)
GoParse-12 15.5MB/s ± 2% 15.5MB/s ± 2% ~ (p=0.702 n=19+20)
RegexpMatchEasy0_32-12 320MB/s ± 1% 318MB/s ± 2% ~ (p=0.094 n=18+20)
RegexpMatchEasy0_1K-12 3.00GB/s ± 1% 2.99GB/s ± 1% ~ (p=0.194 n=20+19)
RegexpMatchEasy1_32-12 388MB/s ± 2% 385MB/s ± 2% -0.83% (p=0.008 n=19+19)
RegexpMatchEasy1_1K-12 2.07GB/s ± 1% 2.07GB/s ± 1% ~ (p=0.964 n=19+18)
RegexpMatchMedium_32-12 7.68MB/s ± 1% 7.64MB/s ± 2% -0.57% (p=0.020 n=19+20)
RegexpMatchMedium_1K-12 26.1MB/s ± 1% 26.1MB/s ± 1% ~ (p=0.211 n=18+18)
RegexpMatchHard_32-12 15.8MB/s ± 1% 15.8MB/s ± 1% ~ (p=0.180 n=18+19)
RegexpMatchHard_1K-12 16.8MB/s ± 1% 16.8MB/s ± 2% ~ (p=0.236 n=20+19)
Revcomp-12 477MB/s ± 1% 475MB/s ± 1% ~ (p=0.071 n=19+17)
Template-12 28.5MB/s ± 2% 26.6MB/s ± 1% -6.77% (p=0.000 n=19+20)
[Geo mean] 100MB/s 99.0MB/s -0.82%
Change-Id: I875bf6ceb306d1ee2f470cabf88aa6ede27c47a0
Reviewed-on: https://go-review.googlesource.com/16059
Reviewed-by: Rick Hudson <rlh@golang.org>
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2015-10-19 13:46:32 -04:00
|
|
|
|
2015-10-26 11:27:37 -04:00
|
|
|
if !gcBlackenPromptly {
|
|
|
|
|
// Transition from mark 1 to mark 2.
|
|
|
|
|
//
|
2015-06-01 18:16:03 -04:00
|
|
|
// The global work list is empty, but there can still be work
|
2016-07-17 22:22:32 -04:00
|
|
|
// sitting in the per-P work caches.
|
|
|
|
|
// Flush and disable work caches.
|
2015-07-27 14:35:38 -04:00
|
|
|
|
2015-10-26 11:27:37 -04:00
|
|
|
// Disallow caching workbufs and indicate that we're in mark 2.
|
|
|
|
|
gcBlackenPromptly = true
|
|
|
|
|
|
|
|
|
|
// Prevent completion of mark 2 until we've flushed
|
|
|
|
|
// cached workbufs.
|
2015-11-02 14:09:24 -05:00
|
|
|
atomic.Xadd(&work.nwait, -1)
|
2015-10-26 11:27:37 -04:00
|
|
|
|
|
|
|
|
// GC is set up for mark 2. Let Gs blocked on the
|
|
|
|
|
// transition lock go while we flush caches.
|
|
|
|
|
semrelease(&work.markDoneSema)
|
|
|
|
|
|
|
|
|
|
systemstack(func() {
|
|
|
|
|
// Flush all currently cached workbufs and
|
|
|
|
|
// ensure all Ps see gcBlackenPromptly. This
|
|
|
|
|
// also blocks until any remaining mark 1
|
|
|
|
|
// workers have exited their loop so we can
|
2016-07-17 22:22:32 -04:00
|
|
|
// start new mark 2 workers.
|
2015-06-01 18:16:03 -04:00
|
|
|
forEachP(func(_p_ *p) {
|
runtime: buffered write barrier implementation
This implements runtime support for buffered write barriers on amd64.
The buffered write barrier has a fast path that simply enqueues
pointers in a per-P buffer. Unlike the current write barrier, this
fast path is *not* a normal Go call and does not require the compiler
to spill general-purpose registers or put arguments on the stack. When
the buffer fills up, the write barrier takes the slow path, which
spills all general purpose registers and flushes the buffer. We don't
allow safe-points or stack splits while this frame is active, so it
doesn't matter that we have no type information for the spilled
registers in this frame.
One minor complication is cgocheck=2 mode, which uses the write
barrier to detect Go pointers being written to non-Go memory. We
obviously can't buffer this, so instead we set the buffer to its
minimum size, forcing the write barrier into the slow path on every
call. For this specific case, we pass additional information as
arguments to the flush function. This also requires enabling the cgo
write barrier slightly later during runtime initialization, after Ps
(and the per-P write barrier buffers) have been initialized.
The code in this CL is not yet active. The next CL will modify the
compiler to generate calls to the new write barrier.
This reduces the average cost of the write barrier by roughly a factor
of 4, which will pay for the cost of having it enabled more of the
time after we make the GC pacer less aggressive. (Benchmarks will be
in the next CL.)
Updates #14951.
Updates #22460.
Change-Id: I396b5b0e2c5e5c4acfd761a3235fd15abadc6cb1
Reviewed-on: https://go-review.googlesource.com/73711
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rick Hudson <rlh@golang.org>
2017-10-26 12:21:16 -04:00
|
|
|
wbBufFlush1(_p_)
|
2015-06-01 18:16:03 -04:00
|
|
|
_p_.gcw.dispose()
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
2017-01-17 21:58:10 -05:00
|
|
|
// Check that roots are marked. We should be able to
|
|
|
|
|
// do this before the forEachP, but based on issue
|
|
|
|
|
// #16083 there may be a (harmless) race where we can
|
|
|
|
|
// enter mark 2 while some workers are still scanning
|
|
|
|
|
// stacks. The forEachP ensures these scans are done.
|
|
|
|
|
//
|
|
|
|
|
// TODO(austin): Figure out the race and fix this
|
|
|
|
|
// properly.
|
|
|
|
|
gcMarkRootCheck()
|
|
|
|
|
|
2015-10-26 11:27:37 -04:00
|
|
|
// Now we can start up mark 2 workers.
|
2015-11-02 14:09:24 -05:00
|
|
|
atomic.Xaddint64(&gcController.dedicatedMarkWorkersNeeded, 0xffffffff)
|
2017-10-05 12:16:45 -04:00
|
|
|
gcController.fractionalUtilizationGoal = prevFractionalGoal
|
2015-03-18 11:22:12 -04:00
|
|
|
|
2015-11-02 14:09:24 -05:00
|
|
|
incnwait := atomic.Xadd(&work.nwait, +1)
|
2015-10-26 11:27:37 -04:00
|
|
|
if incnwait == work.nproc && !gcMarkWorkAvailable(nil) {
|
2015-11-23 11:37:12 -05:00
|
|
|
// This loop will make progress because
|
|
|
|
|
// gcBlackenPromptly is now true, so it won't
|
|
|
|
|
// take this same "if" branch.
|
|
|
|
|
goto top
|
2015-10-26 11:27:37 -04:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// Transition to mark termination.
|
2015-10-23 15:55:03 -04:00
|
|
|
now := nanotime()
|
2015-10-23 15:17:04 -04:00
|
|
|
work.tMarkTerm = now
|
|
|
|
|
work.pauseStart = now
|
2015-11-23 11:37:12 -05:00
|
|
|
getg().m.preemptoff = "gcing"
|
2017-07-21 14:25:28 -04:00
|
|
|
if trace.enabled {
|
|
|
|
|
traceGCSTWStart(0)
|
|
|
|
|
}
|
2015-05-15 16:00:50 -04:00
|
|
|
systemstack(stopTheWorldWithSema)
|
2015-03-18 11:22:12 -04:00
|
|
|
// The gcphase is _GCmark, it will transition to _GCmarktermination
|
|
|
|
|
// below. The important thing is that the wb remains active until
|
|
|
|
|
// all marking is complete. This includes writes made by the GC.
|
2015-03-12 17:56:14 -04:00
|
|
|
|
2016-02-15 18:24:06 -05:00
|
|
|
// Record that one root marking pass has completed.
|
|
|
|
|
work.markrootDone = true
|
runtime: scan objects with finalizers concurrently
This reduces pause time by ~25% relative to tip and by ~50% relative
to Go 1.5.1.
Currently one of the steps of STW mark termination is to loop (in
parallel) over all spans to find objects with finalizers in order to
mark all objects reachable from these objects and to treat the
finalizer special as a root. Unfortunately, even if there are no
finalizers at all, this loop takes roughly 1 ms/heap GB/core, so
multi-gigabyte heaps can quickly push our STW time past 10ms.
Fix this by moving this scan from mark termination to concurrent scan,
where it can run in parallel with mutators. The loop itself could also
be optimized, but this cost is small compared to concurrent marking.
Making this scan concurrent introduces two complications:
1) The scan currently walks the specials list of each span without
locking it, which is safe only with the world stopped. We fix this by
speculatively checking if a span has any specials (the vast majority
won't) and then locking the specials list only if there are specials
to check.
2) An object can have a finalizer set after concurrent scan, in which
case it won't have been marked appropriately by concurrent scan. If
the finalizer is a closure and is only reachable from the special, it
could be swept before it is run. Likewise, if the object is not marked
yet when the finalizer is set and then becomes unreachable before it
is marked, other objects reachable only from it may be swept before
the finalizer function is run. We fix this issue by making
addfinalizer ensure the same marking invariants as markroot does.
For multi-gigabyte heaps, this reduces max pause time by 20%–30%
relative to tip (depending on GOMAXPROCS) and by ~50% relative to Go
1.5.1 (where this loop was neither concurrent nor parallel). Here are
the results for the garbage benchmark:
---------------- max pause ----------------
Heap Procs Concurrent scan STW parallel scan 1.5.1
24GB 12 18ms 23ms 37ms
24GB 4 18ms 25ms 37ms
4GB 4 3.8ms 4.9ms 6.9ms
In all cases, 95%ile pause time is similar to the max pause time. This
also improves mean STW time by 10%–30%.
Fixes #11485.
Change-Id: I9359d8c3d120a51d23d924b52bf853a1299b1dfd
Reviewed-on: https://go-review.googlesource.com/14982
Reviewed-by: Rick Hudson <rlh@golang.org>
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2015-09-24 14:39:27 -04:00
|
|
|
|
2016-01-15 13:28:41 -05:00
|
|
|
// Disable assists and background workers. We must do
|
|
|
|
|
// this before waking blocked assists.
|
|
|
|
|
atomic.Store(&gcBlackenEnabled, 0)
|
|
|
|
|
|
2015-10-14 21:31:33 -04:00
|
|
|
// Wake all blocked assists. These will run when we
|
|
|
|
|
// start the world again.
|
|
|
|
|
gcWakeAllAssists()
|
|
|
|
|
|
2015-10-26 11:27:37 -04:00
|
|
|
// Likewise, release the transition lock. Blocked
|
|
|
|
|
// workers and assists will run when we start the
|
|
|
|
|
// world again.
|
|
|
|
|
semrelease(&work.markDoneSema)
|
|
|
|
|
|
2016-09-11 16:55:34 -04:00
|
|
|
// endCycle depends on all gcWork cache stats being
|
|
|
|
|
// flushed. This is ensured by mark 2.
|
2017-03-31 17:09:41 -04:00
|
|
|
nextTriggerRatio := gcController.endCycle()
|
2015-10-26 11:27:37 -04:00
|
|
|
|
|
|
|
|
// Perform mark termination. This will restart the world.
|
2017-03-31 17:09:41 -04:00
|
|
|
gcMarkTermination(nextTriggerRatio)
|
2014-11-11 17:05:02 -05:00
|
|
|
}
|
2015-10-26 11:27:37 -04:00
|
|
|
}
|
2014-11-11 17:05:02 -05:00
|
|
|
|
2017-03-31 17:09:41 -04:00
|
|
|
func gcMarkTermination(nextTriggerRatio float64) {
|
2015-03-05 17:33:08 -05:00
|
|
|
// World is stopped.
|
|
|
|
|
// Start marktermination which includes enabling the write barrier.
|
2015-11-02 14:09:24 -05:00
|
|
|
atomic.Store(&gcBlackenEnabled, 0)
|
2015-06-01 18:16:03 -04:00
|
|
|
gcBlackenPromptly = false
|
runtime: replace needwb() with writeBarrierEnabled
Reduce the write barrier check to a single load and compare
so that it can be inlined into write barrier use sites.
Makes the standard write barrier a little faster too.
name old new delta
BenchmarkBinaryTree17 17.9s × (0.99,1.01) 17.9s × (1.00,1.01) ~
BenchmarkFannkuch11 4.35s × (1.00,1.00) 4.43s × (1.00,1.00) +1.81%
BenchmarkFmtFprintfEmpty 120ns × (0.93,1.06) 110ns × (1.00,1.06) -7.92%
BenchmarkFmtFprintfString 479ns × (0.99,1.00) 487ns × (0.99,1.00) +1.67%
BenchmarkFmtFprintfInt 452ns × (0.99,1.02) 450ns × (0.99,1.00) ~
BenchmarkFmtFprintfIntInt 766ns × (0.99,1.01) 762ns × (1.00,1.00) ~
BenchmarkFmtFprintfPrefixedInt 576ns × (0.98,1.01) 584ns × (0.99,1.01) ~
BenchmarkFmtFprintfFloat 730ns × (1.00,1.01) 738ns × (1.00,1.00) +1.16%
BenchmarkFmtManyArgs 2.84µs × (0.99,1.00) 2.80µs × (1.00,1.01) -1.22%
BenchmarkGobDecode 39.3ms × (0.98,1.01) 39.0ms × (0.99,1.00) ~
BenchmarkGobEncode 39.5ms × (0.99,1.01) 37.8ms × (0.98,1.01) -4.33%
BenchmarkGzip 663ms × (1.00,1.01) 661ms × (0.99,1.01) ~
BenchmarkGunzip 143ms × (1.00,1.00) 142ms × (1.00,1.00) ~
BenchmarkHTTPClientServer 132µs × (0.99,1.01) 132µs × (0.99,1.01) ~
BenchmarkJSONEncode 57.4ms × (0.99,1.01) 56.3ms × (0.99,1.01) -1.96%
BenchmarkJSONDecode 139ms × (0.99,1.00) 138ms × (0.99,1.01) ~
BenchmarkMandelbrot200 6.03ms × (1.00,1.00) 6.01ms × (1.00,1.00) ~
BenchmarkGoParse 10.3ms × (0.89,1.14) 10.2ms × (0.87,1.05) ~
BenchmarkRegexpMatchEasy0_32 209ns × (1.00,1.00) 208ns × (1.00,1.00) ~
BenchmarkRegexpMatchEasy0_1K 591ns × (0.99,1.00) 588ns × (1.00,1.00) ~
BenchmarkRegexpMatchEasy1_32 184ns × (0.99,1.02) 182ns × (0.99,1.01) ~
BenchmarkRegexpMatchEasy1_1K 1.01µs × (1.00,1.00) 0.99µs × (1.00,1.01) -2.33%
BenchmarkRegexpMatchMedium_32 330ns × (1.00,1.00) 323ns × (1.00,1.01) -2.12%
BenchmarkRegexpMatchMedium_1K 92.6µs × (1.00,1.00) 89.9µs × (1.00,1.00) -2.92%
BenchmarkRegexpMatchHard_32 4.80µs × (0.95,1.00) 4.72µs × (0.95,1.01) ~
BenchmarkRegexpMatchHard_1K 136µs × (1.00,1.00) 133µs × (1.00,1.01) -1.86%
BenchmarkRevcomp 900ms × (0.99,1.04) 900ms × (1.00,1.05) ~
BenchmarkTemplate 172ms × (1.00,1.00) 168ms × (0.99,1.01) -2.07%
BenchmarkTimeParse 637ns × (1.00,1.00) 637ns × (1.00,1.00) ~
BenchmarkTimeFormat 744ns × (1.00,1.01) 738ns × (1.00,1.00) -0.67%
Change-Id: I4ecc925805da1f5ee264377f1f7574f54ee575e7
Reviewed-on: https://go-review.googlesource.com/9321
Reviewed-by: Austin Clements <austin@google.com>
2015-04-24 14:00:55 -04:00
|
|
|
setGCPhase(_GCmarktermination)
|
2015-03-05 17:33:08 -05:00
|
|
|
|
2015-10-23 15:17:04 -04:00
|
|
|
work.heap1 = memstats.heap_live
|
2015-02-19 13:38:46 -05:00
|
|
|
startTime := nanotime()
|
2014-11-11 17:05:02 -05:00
|
|
|
|
2015-03-23 18:15:14 -04:00
|
|
|
mp := acquirem()
|
|
|
|
|
mp.preemptoff = "gcing"
|
2015-02-19 16:43:27 -05:00
|
|
|
_g_ := getg()
|
|
|
|
|
_g_.m.traceback = 2
|
|
|
|
|
gp := _g_.m.curg
|
|
|
|
|
casgstatus(gp, _Grunning, _Gwaiting)
|
|
|
|
|
gp.waitreason = "garbage collection"
|
|
|
|
|
|
2016-03-01 23:21:55 +00:00
|
|
|
// Run gc on the g0 stack. We do this so that the g stack
|
|
|
|
|
// we're currently running on will no longer change. Cuts
|
2015-02-19 13:38:46 -05:00
|
|
|
// the root set down a bit (g0 stacks are not scanned, and
|
|
|
|
|
// we don't need to scan gc's internal state). We also
|
|
|
|
|
// need to switch to g0 so we can shrink the stack.
|
2015-02-19 16:21:00 -05:00
|
|
|
systemstack(func() {
|
2017-04-03 12:10:56 -04:00
|
|
|
gcMark(startTime)
|
2015-07-30 19:39:16 -04:00
|
|
|
// Must return immediately.
|
|
|
|
|
// The outer function's stack may have moved
|
|
|
|
|
// during gcMark (it shrinks stacks, including the
|
|
|
|
|
// outer function's stack), so we must not refer
|
|
|
|
|
// to any of its variables. Return back to the
|
|
|
|
|
// non-system stack to pick up the new addresses
|
|
|
|
|
// before continuing.
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
systemstack(func() {
|
2015-10-23 15:17:04 -04:00
|
|
|
work.heap2 = work.bytesMarked
|
2015-02-19 16:43:27 -05:00
|
|
|
if debug.gccheckmark > 0 {
|
|
|
|
|
// Run a full stop-the-world mark using checkmark bits,
|
|
|
|
|
// to check that we didn't forget to mark anything during
|
|
|
|
|
// the concurrent mark process.
|
2015-06-26 13:56:58 -04:00
|
|
|
gcResetMarkState()
|
2015-02-19 16:43:27 -05:00
|
|
|
initCheckmarks()
|
2017-04-03 12:10:56 -04:00
|
|
|
gcMark(startTime)
|
2015-02-19 16:43:27 -05:00
|
|
|
clearCheckmarks()
|
2015-02-19 15:48:40 -05:00
|
|
|
}
|
2015-03-05 17:33:08 -05:00
|
|
|
|
|
|
|
|
// marking is complete so we can turn the write barrier off
|
runtime: replace needwb() with writeBarrierEnabled
Reduce the write barrier check to a single load and compare
so that it can be inlined into write barrier use sites.
Makes the standard write barrier a little faster too.
name old new delta
BenchmarkBinaryTree17 17.9s × (0.99,1.01) 17.9s × (1.00,1.01) ~
BenchmarkFannkuch11 4.35s × (1.00,1.00) 4.43s × (1.00,1.00) +1.81%
BenchmarkFmtFprintfEmpty 120ns × (0.93,1.06) 110ns × (1.00,1.06) -7.92%
BenchmarkFmtFprintfString 479ns × (0.99,1.00) 487ns × (0.99,1.00) +1.67%
BenchmarkFmtFprintfInt 452ns × (0.99,1.02) 450ns × (0.99,1.00) ~
BenchmarkFmtFprintfIntInt 766ns × (0.99,1.01) 762ns × (1.00,1.00) ~
BenchmarkFmtFprintfPrefixedInt 576ns × (0.98,1.01) 584ns × (0.99,1.01) ~
BenchmarkFmtFprintfFloat 730ns × (1.00,1.01) 738ns × (1.00,1.00) +1.16%
BenchmarkFmtManyArgs 2.84µs × (0.99,1.00) 2.80µs × (1.00,1.01) -1.22%
BenchmarkGobDecode 39.3ms × (0.98,1.01) 39.0ms × (0.99,1.00) ~
BenchmarkGobEncode 39.5ms × (0.99,1.01) 37.8ms × (0.98,1.01) -4.33%
BenchmarkGzip 663ms × (1.00,1.01) 661ms × (0.99,1.01) ~
BenchmarkGunzip 143ms × (1.00,1.00) 142ms × (1.00,1.00) ~
BenchmarkHTTPClientServer 132µs × (0.99,1.01) 132µs × (0.99,1.01) ~
BenchmarkJSONEncode 57.4ms × (0.99,1.01) 56.3ms × (0.99,1.01) -1.96%
BenchmarkJSONDecode 139ms × (0.99,1.00) 138ms × (0.99,1.01) ~
BenchmarkMandelbrot200 6.03ms × (1.00,1.00) 6.01ms × (1.00,1.00) ~
BenchmarkGoParse 10.3ms × (0.89,1.14) 10.2ms × (0.87,1.05) ~
BenchmarkRegexpMatchEasy0_32 209ns × (1.00,1.00) 208ns × (1.00,1.00) ~
BenchmarkRegexpMatchEasy0_1K 591ns × (0.99,1.00) 588ns × (1.00,1.00) ~
BenchmarkRegexpMatchEasy1_32 184ns × (0.99,1.02) 182ns × (0.99,1.01) ~
BenchmarkRegexpMatchEasy1_1K 1.01µs × (1.00,1.00) 0.99µs × (1.00,1.01) -2.33%
BenchmarkRegexpMatchMedium_32 330ns × (1.00,1.00) 323ns × (1.00,1.01) -2.12%
BenchmarkRegexpMatchMedium_1K 92.6µs × (1.00,1.00) 89.9µs × (1.00,1.00) -2.92%
BenchmarkRegexpMatchHard_32 4.80µs × (0.95,1.00) 4.72µs × (0.95,1.01) ~
BenchmarkRegexpMatchHard_1K 136µs × (1.00,1.00) 133µs × (1.00,1.01) -1.86%
BenchmarkRevcomp 900ms × (0.99,1.04) 900ms × (1.00,1.05) ~
BenchmarkTemplate 172ms × (1.00,1.00) 168ms × (0.99,1.01) -2.07%
BenchmarkTimeParse 637ns × (1.00,1.00) 637ns × (1.00,1.00) ~
BenchmarkTimeFormat 744ns × (1.00,1.01) 738ns × (1.00,1.00) -0.67%
Change-Id: I4ecc925805da1f5ee264377f1f7574f54ee575e7
Reviewed-on: https://go-review.googlesource.com/9321
Reviewed-by: Austin Clements <austin@google.com>
2015-04-24 14:00:55 -04:00
|
|
|
setGCPhase(_GCoff)
|
2015-10-23 15:17:04 -04:00
|
|
|
gcSweep(work.mode)
|
2015-02-19 15:48:40 -05:00
|
|
|
|
2015-02-19 16:43:27 -05:00
|
|
|
if debug.gctrace > 1 {
|
|
|
|
|
startTime = nanotime()
|
2015-02-24 17:49:55 -05:00
|
|
|
// The g stacks have been scanned so
|
|
|
|
|
// they have gcscanvalid==true and gcworkdone==true.
|
|
|
|
|
// Reset these so that all stacks will be rescanned.
|
2015-06-26 13:56:58 -04:00
|
|
|
gcResetMarkState()
|
2016-10-05 21:22:33 -04:00
|
|
|
finishsweep_m()
|
2015-03-05 17:33:08 -05:00
|
|
|
|
|
|
|
|
// Still in STW but gcphase is _GCoff, reset to _GCmarktermination
|
|
|
|
|
// At this point all objects will be found during the gcMark which
|
|
|
|
|
// does a complete STW mark and object scan.
|
runtime: replace needwb() with writeBarrierEnabled
Reduce the write barrier check to a single load and compare
so that it can be inlined into write barrier use sites.
Makes the standard write barrier a little faster too.
name old new delta
BenchmarkBinaryTree17 17.9s × (0.99,1.01) 17.9s × (1.00,1.01) ~
BenchmarkFannkuch11 4.35s × (1.00,1.00) 4.43s × (1.00,1.00) +1.81%
BenchmarkFmtFprintfEmpty 120ns × (0.93,1.06) 110ns × (1.00,1.06) -7.92%
BenchmarkFmtFprintfString 479ns × (0.99,1.00) 487ns × (0.99,1.00) +1.67%
BenchmarkFmtFprintfInt 452ns × (0.99,1.02) 450ns × (0.99,1.00) ~
BenchmarkFmtFprintfIntInt 766ns × (0.99,1.01) 762ns × (1.00,1.00) ~
BenchmarkFmtFprintfPrefixedInt 576ns × (0.98,1.01) 584ns × (0.99,1.01) ~
BenchmarkFmtFprintfFloat 730ns × (1.00,1.01) 738ns × (1.00,1.00) +1.16%
BenchmarkFmtManyArgs 2.84µs × (0.99,1.00) 2.80µs × (1.00,1.01) -1.22%
BenchmarkGobDecode 39.3ms × (0.98,1.01) 39.0ms × (0.99,1.00) ~
BenchmarkGobEncode 39.5ms × (0.99,1.01) 37.8ms × (0.98,1.01) -4.33%
BenchmarkGzip 663ms × (1.00,1.01) 661ms × (0.99,1.01) ~
BenchmarkGunzip 143ms × (1.00,1.00) 142ms × (1.00,1.00) ~
BenchmarkHTTPClientServer 132µs × (0.99,1.01) 132µs × (0.99,1.01) ~
BenchmarkJSONEncode 57.4ms × (0.99,1.01) 56.3ms × (0.99,1.01) -1.96%
BenchmarkJSONDecode 139ms × (0.99,1.00) 138ms × (0.99,1.01) ~
BenchmarkMandelbrot200 6.03ms × (1.00,1.00) 6.01ms × (1.00,1.00) ~
BenchmarkGoParse 10.3ms × (0.89,1.14) 10.2ms × (0.87,1.05) ~
BenchmarkRegexpMatchEasy0_32 209ns × (1.00,1.00) 208ns × (1.00,1.00) ~
BenchmarkRegexpMatchEasy0_1K 591ns × (0.99,1.00) 588ns × (1.00,1.00) ~
BenchmarkRegexpMatchEasy1_32 184ns × (0.99,1.02) 182ns × (0.99,1.01) ~
BenchmarkRegexpMatchEasy1_1K 1.01µs × (1.00,1.00) 0.99µs × (1.00,1.01) -2.33%
BenchmarkRegexpMatchMedium_32 330ns × (1.00,1.00) 323ns × (1.00,1.01) -2.12%
BenchmarkRegexpMatchMedium_1K 92.6µs × (1.00,1.00) 89.9µs × (1.00,1.00) -2.92%
BenchmarkRegexpMatchHard_32 4.80µs × (0.95,1.00) 4.72µs × (0.95,1.01) ~
BenchmarkRegexpMatchHard_1K 136µs × (1.00,1.00) 133µs × (1.00,1.01) -1.86%
BenchmarkRevcomp 900ms × (0.99,1.04) 900ms × (1.00,1.05) ~
BenchmarkTemplate 172ms × (1.00,1.00) 168ms × (0.99,1.01) -2.07%
BenchmarkTimeParse 637ns × (1.00,1.00) 637ns × (1.00,1.00) ~
BenchmarkTimeFormat 744ns × (1.00,1.01) 738ns × (1.00,1.00) -0.67%
Change-Id: I4ecc925805da1f5ee264377f1f7574f54ee575e7
Reviewed-on: https://go-review.googlesource.com/9321
Reviewed-by: Austin Clements <austin@google.com>
2015-04-24 14:00:55 -04:00
|
|
|
setGCPhase(_GCmarktermination)
|
2017-04-03 12:10:56 -04:00
|
|
|
gcMark(startTime)
|
runtime: replace needwb() with writeBarrierEnabled
Reduce the write barrier check to a single load and compare
so that it can be inlined into write barrier use sites.
Makes the standard write barrier a little faster too.
name old new delta
BenchmarkBinaryTree17 17.9s × (0.99,1.01) 17.9s × (1.00,1.01) ~
BenchmarkFannkuch11 4.35s × (1.00,1.00) 4.43s × (1.00,1.00) +1.81%
BenchmarkFmtFprintfEmpty 120ns × (0.93,1.06) 110ns × (1.00,1.06) -7.92%
BenchmarkFmtFprintfString 479ns × (0.99,1.00) 487ns × (0.99,1.00) +1.67%
BenchmarkFmtFprintfInt 452ns × (0.99,1.02) 450ns × (0.99,1.00) ~
BenchmarkFmtFprintfIntInt 766ns × (0.99,1.01) 762ns × (1.00,1.00) ~
BenchmarkFmtFprintfPrefixedInt 576ns × (0.98,1.01) 584ns × (0.99,1.01) ~
BenchmarkFmtFprintfFloat 730ns × (1.00,1.01) 738ns × (1.00,1.00) +1.16%
BenchmarkFmtManyArgs 2.84µs × (0.99,1.00) 2.80µs × (1.00,1.01) -1.22%
BenchmarkGobDecode 39.3ms × (0.98,1.01) 39.0ms × (0.99,1.00) ~
BenchmarkGobEncode 39.5ms × (0.99,1.01) 37.8ms × (0.98,1.01) -4.33%
BenchmarkGzip 663ms × (1.00,1.01) 661ms × (0.99,1.01) ~
BenchmarkGunzip 143ms × (1.00,1.00) 142ms × (1.00,1.00) ~
BenchmarkHTTPClientServer 132µs × (0.99,1.01) 132µs × (0.99,1.01) ~
BenchmarkJSONEncode 57.4ms × (0.99,1.01) 56.3ms × (0.99,1.01) -1.96%
BenchmarkJSONDecode 139ms × (0.99,1.00) 138ms × (0.99,1.01) ~
BenchmarkMandelbrot200 6.03ms × (1.00,1.00) 6.01ms × (1.00,1.00) ~
BenchmarkGoParse 10.3ms × (0.89,1.14) 10.2ms × (0.87,1.05) ~
BenchmarkRegexpMatchEasy0_32 209ns × (1.00,1.00) 208ns × (1.00,1.00) ~
BenchmarkRegexpMatchEasy0_1K 591ns × (0.99,1.00) 588ns × (1.00,1.00) ~
BenchmarkRegexpMatchEasy1_32 184ns × (0.99,1.02) 182ns × (0.99,1.01) ~
BenchmarkRegexpMatchEasy1_1K 1.01µs × (1.00,1.00) 0.99µs × (1.00,1.01) -2.33%
BenchmarkRegexpMatchMedium_32 330ns × (1.00,1.00) 323ns × (1.00,1.01) -2.12%
BenchmarkRegexpMatchMedium_1K 92.6µs × (1.00,1.00) 89.9µs × (1.00,1.00) -2.92%
BenchmarkRegexpMatchHard_32 4.80µs × (0.95,1.00) 4.72µs × (0.95,1.01) ~
BenchmarkRegexpMatchHard_1K 136µs × (1.00,1.00) 133µs × (1.00,1.01) -1.86%
BenchmarkRevcomp 900ms × (0.99,1.04) 900ms × (1.00,1.05) ~
BenchmarkTemplate 172ms × (1.00,1.00) 168ms × (0.99,1.01) -2.07%
BenchmarkTimeParse 637ns × (1.00,1.00) 637ns × (1.00,1.00) ~
BenchmarkTimeFormat 744ns × (1.00,1.01) 738ns × (1.00,1.00) -0.67%
Change-Id: I4ecc925805da1f5ee264377f1f7574f54ee575e7
Reviewed-on: https://go-review.googlesource.com/9321
Reviewed-by: Austin Clements <austin@google.com>
2015-04-24 14:00:55 -04:00
|
|
|
setGCPhase(_GCoff) // marking is done, turn off wb.
|
2015-10-23 15:17:04 -04:00
|
|
|
gcSweep(work.mode)
|
2015-02-19 15:48:40 -05:00
|
|
|
}
|
2015-02-19 13:38:46 -05:00
|
|
|
})
|
2014-11-11 17:05:02 -05:00
|
|
|
|
2015-02-19 16:43:27 -05:00
|
|
|
_g_.m.traceback = 0
|
|
|
|
|
casgstatus(gp, _Gwaiting, _Grunning)
|
2015-02-19 16:21:00 -05:00
|
|
|
|
2015-02-19 13:38:46 -05:00
|
|
|
if trace.enabled {
|
|
|
|
|
traceGCDone()
|
|
|
|
|
}
|
2014-11-11 17:05:02 -05:00
|
|
|
|
2015-02-19 13:38:46 -05:00
|
|
|
// all done
|
|
|
|
|
mp.preemptoff = ""
|
2014-11-11 17:05:02 -05:00
|
|
|
|
2015-03-05 17:33:08 -05:00
|
|
|
if gcphase != _GCoff {
|
|
|
|
|
throw("gc done but gcphase != _GCoff")
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-03 12:10:56 -04:00
|
|
|
// Update GC trigger and pacing for the next cycle.
|
|
|
|
|
gcSetTriggerRatio(nextTriggerRatio)
|
|
|
|
|
|
2015-07-01 11:04:19 -04:00
|
|
|
// Update timing memstats
|
2017-02-03 19:26:13 -05:00
|
|
|
now := nanotime()
|
|
|
|
|
sec, nsec, _ := time_now()
|
|
|
|
|
unixNow := sec*1e9 + int64(nsec)
|
2015-10-23 15:17:04 -04:00
|
|
|
work.pauseNS += now - work.pauseStart
|
|
|
|
|
work.tEnd = now
|
2017-02-03 19:26:13 -05:00
|
|
|
atomic.Store64(&memstats.last_gc_unix, uint64(unixNow)) // must be Unix time to make sense to user
|
|
|
|
|
atomic.Store64(&memstats.last_gc_nanotime, uint64(now)) // monotonic time for us
|
2015-10-23 15:17:04 -04:00
|
|
|
memstats.pause_ns[memstats.numgc%uint32(len(memstats.pause_ns))] = uint64(work.pauseNS)
|
2015-07-01 11:04:19 -04:00
|
|
|
memstats.pause_end[memstats.numgc%uint32(len(memstats.pause_end))] = uint64(unixNow)
|
2015-10-23 15:17:04 -04:00
|
|
|
memstats.pause_total_ns += uint64(work.pauseNS)
|
2015-07-01 11:04:19 -04:00
|
|
|
|
2015-07-29 14:02:34 -04:00
|
|
|
// Update work.totaltime.
|
2015-10-23 15:17:04 -04:00
|
|
|
sweepTermCpu := int64(work.stwprocs) * (work.tMark - work.tSweepTerm)
|
2015-07-29 14:02:34 -04:00
|
|
|
// We report idle marking time below, but omit it from the
|
|
|
|
|
// overall utilization here since it's "free".
|
|
|
|
|
markCpu := gcController.assistTime + gcController.dedicatedMarkTime + gcController.fractionalMarkTime
|
2015-10-23 15:17:04 -04:00
|
|
|
markTermCpu := int64(work.stwprocs) * (work.tEnd - work.tMarkTerm)
|
runtime: perform concurrent scan in GC workers
Currently the concurrent root scan is performed in its entirety by the
GC coordinator before entering concurrent mark (which enables GC
workers). This scan is done sequentially, which can prolong the scan
phase, delay the mark phase, and means that the scan phase does not
obey the 25% CPU goal. Furthermore, there's no need to complete the
root scan before starting marking (in fact, we already allow GC
assists to happen during the scan phase), so this acts as an
unnecessary barrier between root scanning and marking.
This change shifts the root scan work out of the GC coordinator and in
to the GC workers. The coordinator simply sets up the scan state and
enqueues the right number of root scan jobs. The GC workers then drain
the root scan jobs prior to draining heap scan jobs.
This parallelizes the root scan process, makes it obey the 25% CPU
goal, and effectively eliminates root scanning as an isolated phase,
allowing the system to smoothly transition from root scanning to heap
marking. This also eliminates a major non-STW responsibility of the GC
coordinator, which will make it easier to switch to a decentralized
state machine. Finally, it puts us in a good position to perform root
scanning in assists as well, which will help satisfy assists at the
beginning of the GC cycle.
This is mostly straightforward. One tricky aspect is that we have to
deal with preemption deadlock: where two non-preemptible gorountines
are trying to preempt each other to perform a stack scan. Given the
context where this happens, the only instance of this is two
background workers trying to scan each other. We avoid this by simply
not scanning the stacks of background workers during the concurrent
phase; this is safe because we'll scan them during mark termination
(and their stacks are *very* small and should not contain any new
pointers).
This change also switches the root marking during mark termination to
use the same gcDrain-based code path as concurrent mark. This
shouldn't affect performance because STW root marking was already
parallel and tasks switched to heap marking immediately when no more
root marking tasks were available. However, it simplifies the code and
unifies these code paths.
This has negligible effect on the go1 benchmarks. It slightly slows
down the garbage benchmark, possibly by making GC run slightly more
frequently.
name old time/op new time/op delta
XBenchGarbage-12 5.10ms ± 1% 5.24ms ± 1% +2.87% (p=0.000 n=18+18)
name old time/op new time/op delta
BinaryTree17-12 3.25s ± 3% 3.20s ± 5% -1.57% (p=0.013 n=20+20)
Fannkuch11-12 2.45s ± 1% 2.46s ± 1% +0.38% (p=0.019 n=20+18)
FmtFprintfEmpty-12 49.7ns ± 3% 49.9ns ± 4% ~ (p=0.851 n=19+20)
FmtFprintfString-12 170ns ± 2% 170ns ± 1% ~ (p=0.775 n=20+19)
FmtFprintfInt-12 161ns ± 1% 160ns ± 1% -0.78% (p=0.000 n=19+18)
FmtFprintfIntInt-12 267ns ± 1% 270ns ± 1% +1.04% (p=0.000 n=19+19)
FmtFprintfPrefixedInt-12 238ns ± 2% 238ns ± 1% ~ (p=0.133 n=18+19)
FmtFprintfFloat-12 311ns ± 1% 310ns ± 2% -0.35% (p=0.023 n=20+19)
FmtManyArgs-12 1.08µs ± 1% 1.06µs ± 1% -2.31% (p=0.000 n=20+20)
GobDecode-12 8.65ms ± 1% 8.63ms ± 1% ~ (p=0.377 n=18+20)
GobEncode-12 6.49ms ± 1% 6.52ms ± 1% +0.37% (p=0.015 n=20+20)
Gzip-12 319ms ± 3% 318ms ± 1% ~ (p=0.975 n=19+17)
Gunzip-12 41.9ms ± 1% 42.1ms ± 2% +0.65% (p=0.004 n=19+20)
HTTPClientServer-12 61.7µs ± 1% 62.6µs ± 1% +1.40% (p=0.000 n=18+20)
JSONEncode-12 16.8ms ± 1% 16.9ms ± 1% ~ (p=0.239 n=20+18)
JSONDecode-12 58.4ms ± 1% 60.7ms ± 1% +3.85% (p=0.000 n=19+20)
Mandelbrot200-12 3.86ms ± 0% 3.86ms ± 1% ~ (p=0.092 n=18+19)
GoParse-12 3.75ms ± 2% 3.75ms ± 2% ~ (p=0.708 n=19+20)
RegexpMatchEasy0_32-12 100ns ± 1% 100ns ± 2% +0.60% (p=0.010 n=17+20)
RegexpMatchEasy0_1K-12 341ns ± 1% 342ns ± 2% ~ (p=0.203 n=20+19)
RegexpMatchEasy1_32-12 82.5ns ± 2% 83.2ns ± 2% +0.83% (p=0.007 n=19+19)
RegexpMatchEasy1_1K-12 495ns ± 1% 495ns ± 2% ~ (p=0.970 n=19+18)
RegexpMatchMedium_32-12 130ns ± 2% 130ns ± 2% +0.59% (p=0.039 n=19+20)
RegexpMatchMedium_1K-12 39.2µs ± 1% 39.3µs ± 1% ~ (p=0.214 n=18+18)
RegexpMatchHard_32-12 2.03µs ± 2% 2.02µs ± 1% ~ (p=0.166 n=18+19)
RegexpMatchHard_1K-12 61.0µs ± 1% 60.9µs ± 1% ~ (p=0.169 n=20+18)
Revcomp-12 533ms ± 1% 535ms ± 1% ~ (p=0.071 n=19+17)
Template-12 68.1ms ± 2% 73.0ms ± 1% +7.26% (p=0.000 n=19+20)
TimeParse-12 355ns ± 2% 356ns ± 2% ~ (p=0.530 n=19+20)
TimeFormat-12 357ns ± 2% 347ns ± 1% -2.59% (p=0.000 n=20+19)
[Geo mean] 62.1µs 62.3µs +0.31%
name old speed new speed delta
GobDecode-12 88.7MB/s ± 1% 88.9MB/s ± 1% ~ (p=0.377 n=18+20)
GobEncode-12 118MB/s ± 1% 118MB/s ± 1% -0.37% (p=0.015 n=20+20)
Gzip-12 60.9MB/s ± 3% 60.9MB/s ± 1% ~ (p=0.944 n=19+17)
Gunzip-12 464MB/s ± 1% 461MB/s ± 2% -0.64% (p=0.004 n=19+20)
JSONEncode-12 115MB/s ± 1% 115MB/s ± 1% ~ (p=0.236 n=20+18)
JSONDecode-12 33.2MB/s ± 1% 32.0MB/s ± 1% -3.71% (p=0.000 n=19+20)
GoParse-12 15.5MB/s ± 2% 15.5MB/s ± 2% ~ (p=0.702 n=19+20)
RegexpMatchEasy0_32-12 320MB/s ± 1% 318MB/s ± 2% ~ (p=0.094 n=18+20)
RegexpMatchEasy0_1K-12 3.00GB/s ± 1% 2.99GB/s ± 1% ~ (p=0.194 n=20+19)
RegexpMatchEasy1_32-12 388MB/s ± 2% 385MB/s ± 2% -0.83% (p=0.008 n=19+19)
RegexpMatchEasy1_1K-12 2.07GB/s ± 1% 2.07GB/s ± 1% ~ (p=0.964 n=19+18)
RegexpMatchMedium_32-12 7.68MB/s ± 1% 7.64MB/s ± 2% -0.57% (p=0.020 n=19+20)
RegexpMatchMedium_1K-12 26.1MB/s ± 1% 26.1MB/s ± 1% ~ (p=0.211 n=18+18)
RegexpMatchHard_32-12 15.8MB/s ± 1% 15.8MB/s ± 1% ~ (p=0.180 n=18+19)
RegexpMatchHard_1K-12 16.8MB/s ± 1% 16.8MB/s ± 2% ~ (p=0.236 n=20+19)
Revcomp-12 477MB/s ± 1% 475MB/s ± 1% ~ (p=0.071 n=19+17)
Template-12 28.5MB/s ± 2% 26.6MB/s ± 1% -6.77% (p=0.000 n=19+20)
[Geo mean] 100MB/s 99.0MB/s -0.82%
Change-Id: I875bf6ceb306d1ee2f470cabf88aa6ede27c47a0
Reviewed-on: https://go-review.googlesource.com/16059
Reviewed-by: Rick Hudson <rlh@golang.org>
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2015-10-19 13:46:32 -04:00
|
|
|
cycleCpu := sweepTermCpu + markCpu + markTermCpu
|
2015-07-29 14:02:34 -04:00
|
|
|
work.totaltime += cycleCpu
|
|
|
|
|
|
|
|
|
|
// Compute overall GC CPU utilization.
|
|
|
|
|
totalCpu := sched.totaltime + (now-sched.procresizetime)*int64(gomaxprocs)
|
|
|
|
|
memstats.gc_cpu_fraction = float64(work.totaltime) / float64(totalCpu)
|
|
|
|
|
|
2015-12-14 15:07:40 -05:00
|
|
|
// Reset sweep state.
|
|
|
|
|
sweep.nbgsweep = 0
|
|
|
|
|
sweep.npausesweep = 0
|
|
|
|
|
|
2017-02-27 10:46:12 -05:00
|
|
|
if work.userForced {
|
|
|
|
|
memstats.numforcedgc++
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-23 21:50:19 -05:00
|
|
|
// Bump GC cycle count and wake goroutines waiting on sweep.
|
|
|
|
|
lock(&work.sweepWaiters.lock)
|
|
|
|
|
memstats.numgc++
|
|
|
|
|
injectglist(work.sweepWaiters.head.ptr())
|
|
|
|
|
work.sweepWaiters.head = 0
|
|
|
|
|
unlock(&work.sweepWaiters.lock)
|
|
|
|
|
|
2017-03-01 21:03:20 -05:00
|
|
|
// Finish the current heap profiling cycle and start a new
|
|
|
|
|
// heap profiling cycle. We do this before starting the world
|
|
|
|
|
// so events don't leak into the wrong cycle.
|
|
|
|
|
mProf_NextCycle()
|
2017-03-01 13:58:22 -05:00
|
|
|
|
2017-07-21 14:25:28 -04:00
|
|
|
systemstack(func() { startTheWorldWithSema(true) })
|
2015-10-27 17:48:18 -04:00
|
|
|
|
2017-03-01 13:58:22 -05:00
|
|
|
// Flush the heap profile so we can start a new cycle next GC.
|
|
|
|
|
// This is relatively expensive, so we don't do it with the
|
|
|
|
|
// world stopped.
|
2017-03-01 21:03:20 -05:00
|
|
|
mProf_Flush()
|
2016-09-11 20:03:14 -04:00
|
|
|
|
2017-03-20 17:25:59 -04:00
|
|
|
// Prepare workbufs for freeing by the sweeper. We do this
|
|
|
|
|
// asynchronously because it can take non-trivial time.
|
|
|
|
|
prepareFreeWorkbufs()
|
|
|
|
|
|
2015-10-27 17:48:18 -04:00
|
|
|
// Free stack spans. This must be done between GC cycles.
|
|
|
|
|
systemstack(freeStackSpans)
|
|
|
|
|
|
2015-12-14 15:19:07 -05:00
|
|
|
// Print gctrace before dropping worldsema. As soon as we drop
|
|
|
|
|
// worldsema another cycle could start and smash the stats
|
|
|
|
|
// we're trying to print.
|
2015-03-26 18:48:42 -04:00
|
|
|
if debug.gctrace > 0 {
|
2015-07-29 14:02:34 -04:00
|
|
|
util := int(memstats.gc_cpu_fraction * 100)
|
2015-04-01 13:47:35 -04:00
|
|
|
|
2015-03-26 18:48:42 -04:00
|
|
|
var sbuf [24]byte
|
|
|
|
|
printlock()
|
2015-07-20 15:48:53 -04:00
|
|
|
print("gc ", memstats.numgc,
|
2015-10-23 15:17:04 -04:00
|
|
|
" @", string(itoaDiv(sbuf[:], uint64(work.tSweepTerm-runtimeInitTime)/1e6, 3)), "s ",
|
runtime: increase precision of gctrace times
Currently we truncate gctrace clock and CPU times to millisecond
precision. As a result, many phases are typically printed as 0, which
is fine for user consumption, but makes gathering statistics and
reports over GC traces difficult.
In 1.4, the gctrace line printed times in microseconds. This was
better for statistics, but not as easy for users to read or interpret,
and it generally made the trace lines longer.
This change strikes a balance between these extremes by printing
milliseconds, but including the decimal part to two significant
figures down to microsecond precision. This remains easy to read and
interpret, but includes more precision when it's useful.
For example, where the code currently prints,
gc #29 @1.629s 0%: 0+2+0+12+0 ms clock, 0+2+0+0/12/0+0 ms cpu, 4->4->2 MB, 4 MB goal, 1 P
this prints,
gc #29 @1.629s 0%: 0.005+2.1+0+12+0.29 ms clock, 0.005+2.1+0+0/12/0+0.29 ms cpu, 4->4->2 MB, 4 MB goal, 1 P
Fixes #10970.
Change-Id: I249624779433927cd8b0947b986df9060c289075
Reviewed-on: https://go-review.googlesource.com/10554
Reviewed-by: Russ Cox <rsc@golang.org>
2015-05-30 21:47:00 -04:00
|
|
|
util, "%: ")
|
2015-10-23 15:17:04 -04:00
|
|
|
prev := work.tSweepTerm
|
2016-01-08 14:57:26 -05:00
|
|
|
for i, ns := range []int64{work.tMark, work.tMarkTerm, work.tEnd} {
|
runtime: increase precision of gctrace times
Currently we truncate gctrace clock and CPU times to millisecond
precision. As a result, many phases are typically printed as 0, which
is fine for user consumption, but makes gathering statistics and
reports over GC traces difficult.
In 1.4, the gctrace line printed times in microseconds. This was
better for statistics, but not as easy for users to read or interpret,
and it generally made the trace lines longer.
This change strikes a balance between these extremes by printing
milliseconds, but including the decimal part to two significant
figures down to microsecond precision. This remains easy to read and
interpret, but includes more precision when it's useful.
For example, where the code currently prints,
gc #29 @1.629s 0%: 0+2+0+12+0 ms clock, 0+2+0+0/12/0+0 ms cpu, 4->4->2 MB, 4 MB goal, 1 P
this prints,
gc #29 @1.629s 0%: 0.005+2.1+0+12+0.29 ms clock, 0.005+2.1+0+0/12/0+0.29 ms cpu, 4->4->2 MB, 4 MB goal, 1 P
Fixes #10970.
Change-Id: I249624779433927cd8b0947b986df9060c289075
Reviewed-on: https://go-review.googlesource.com/10554
Reviewed-by: Russ Cox <rsc@golang.org>
2015-05-30 21:47:00 -04:00
|
|
|
if i != 0 {
|
|
|
|
|
print("+")
|
|
|
|
|
}
|
|
|
|
|
print(string(fmtNSAsMS(sbuf[:], uint64(ns-prev))))
|
|
|
|
|
prev = ns
|
|
|
|
|
}
|
|
|
|
|
print(" ms clock, ")
|
2016-01-08 14:57:26 -05:00
|
|
|
for i, ns := range []int64{sweepTermCpu, gcController.assistTime, gcController.dedicatedMarkTime + gcController.fractionalMarkTime, gcController.idleMarkTime, markTermCpu} {
|
|
|
|
|
if i == 2 || i == 3 {
|
runtime: increase precision of gctrace times
Currently we truncate gctrace clock and CPU times to millisecond
precision. As a result, many phases are typically printed as 0, which
is fine for user consumption, but makes gathering statistics and
reports over GC traces difficult.
In 1.4, the gctrace line printed times in microseconds. This was
better for statistics, but not as easy for users to read or interpret,
and it generally made the trace lines longer.
This change strikes a balance between these extremes by printing
milliseconds, but including the decimal part to two significant
figures down to microsecond precision. This remains easy to read and
interpret, but includes more precision when it's useful.
For example, where the code currently prints,
gc #29 @1.629s 0%: 0+2+0+12+0 ms clock, 0+2+0+0/12/0+0 ms cpu, 4->4->2 MB, 4 MB goal, 1 P
this prints,
gc #29 @1.629s 0%: 0.005+2.1+0+12+0.29 ms clock, 0.005+2.1+0+0/12/0+0.29 ms cpu, 4->4->2 MB, 4 MB goal, 1 P
Fixes #10970.
Change-Id: I249624779433927cd8b0947b986df9060c289075
Reviewed-on: https://go-review.googlesource.com/10554
Reviewed-by: Russ Cox <rsc@golang.org>
2015-05-30 21:47:00 -04:00
|
|
|
// Separate mark time components with /.
|
|
|
|
|
print("/")
|
|
|
|
|
} else if i != 0 {
|
|
|
|
|
print("+")
|
|
|
|
|
}
|
|
|
|
|
print(string(fmtNSAsMS(sbuf[:], uint64(ns))))
|
|
|
|
|
}
|
|
|
|
|
print(" ms cpu, ",
|
2015-10-23 15:17:04 -04:00
|
|
|
work.heap0>>20, "->", work.heap1>>20, "->", work.heap2>>20, " MB, ",
|
|
|
|
|
work.heapGoal>>20, " MB goal, ",
|
|
|
|
|
work.maxprocs, " P")
|
2017-02-27 10:46:12 -05:00
|
|
|
if work.userForced {
|
2015-03-26 18:48:42 -04:00
|
|
|
print(" (forced)")
|
|
|
|
|
}
|
|
|
|
|
print("\n")
|
|
|
|
|
printunlock()
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-14 15:19:07 -05:00
|
|
|
semrelease(&worldsema)
|
|
|
|
|
// Careful: another GC cycle may start now.
|
|
|
|
|
|
|
|
|
|
releasem(mp)
|
|
|
|
|
mp = nil
|
|
|
|
|
|
2015-02-19 13:38:46 -05:00
|
|
|
// now that gc is done, kick off finalizer thread if needed
|
|
|
|
|
if !concurrentSweep {
|
|
|
|
|
// give the queued finalizers, if any, a chance to run
|
|
|
|
|
Gosched()
|
2014-11-11 17:05:02 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
runtime: multi-threaded, utilization-scheduled background mark
Currently, the concurrent mark phase is performed by the main GC
goroutine. Prior to the previous commit enabling preemption, this
caused marking to always consume 1/GOMAXPROCS of the available CPU
time. If GOMAXPROCS=1, this meant background GC would consume 100% of
the CPU (effectively a STW). If GOMAXPROCS>4, background GC would use
less than the goal of 25%. If GOMAXPROCS=4, background GC would use
the goal 25%, but if the mutator wasn't using the remaining 75%,
background marking wouldn't take advantage of the idle time. Enabling
preemption in the previous commit made GC miss CPU targets in
completely different ways, but set us up to bring everything back in
line.
This change replaces the fixed GC goroutine with per-P background mark
goroutines. Once started, these goroutines don't go in the standard
run queues; instead, they are scheduled specially such that the time
spent in mutator assists and the background mark goroutines totals 25%
of the CPU time available to the program. Furthermore, this lets
background marking take advantage of idle Ps, which significantly
boosts GC performance for applications that under-utilize the CPU.
This requires also changing how time is reported for gctrace, so this
change splits the concurrent mark CPU time into assist/background/idle
scanning.
This also requires increasing the size of the StackRecord slice used
in a GoroutineProfile test.
Change-Id: I0936ff907d2cee6cb687a208f2df47e8988e3157
Reviewed-on: https://go-review.googlesource.com/8850
Reviewed-by: Rick Hudson <rlh@golang.org>
2015-03-23 21:07:33 -04:00
|
|
|
// gcBgMarkStartWorkers prepares background mark worker goroutines.
|
|
|
|
|
// These goroutines will not run until the mark phase, but they must
|
|
|
|
|
// be started while the work is not stopped and from a regular G
|
|
|
|
|
// stack. The caller must hold worldsema.
|
|
|
|
|
func gcBgMarkStartWorkers() {
|
|
|
|
|
// Background marking is performed by per-P G's. Ensure that
|
|
|
|
|
// each P has a background GC G.
|
2017-06-13 11:32:17 -04:00
|
|
|
for _, p := range allp {
|
2016-01-26 14:44:58 -05:00
|
|
|
if p.gcBgMarkWorker == 0 {
|
runtime: multi-threaded, utilization-scheduled background mark
Currently, the concurrent mark phase is performed by the main GC
goroutine. Prior to the previous commit enabling preemption, this
caused marking to always consume 1/GOMAXPROCS of the available CPU
time. If GOMAXPROCS=1, this meant background GC would consume 100% of
the CPU (effectively a STW). If GOMAXPROCS>4, background GC would use
less than the goal of 25%. If GOMAXPROCS=4, background GC would use
the goal 25%, but if the mutator wasn't using the remaining 75%,
background marking wouldn't take advantage of the idle time. Enabling
preemption in the previous commit made GC miss CPU targets in
completely different ways, but set us up to bring everything back in
line.
This change replaces the fixed GC goroutine with per-P background mark
goroutines. Once started, these goroutines don't go in the standard
run queues; instead, they are scheduled specially such that the time
spent in mutator assists and the background mark goroutines totals 25%
of the CPU time available to the program. Furthermore, this lets
background marking take advantage of idle Ps, which significantly
boosts GC performance for applications that under-utilize the CPU.
This requires also changing how time is reported for gctrace, so this
change splits the concurrent mark CPU time into assist/background/idle
scanning.
This also requires increasing the size of the StackRecord slice used
in a GoroutineProfile test.
Change-Id: I0936ff907d2cee6cb687a208f2df47e8988e3157
Reviewed-on: https://go-review.googlesource.com/8850
Reviewed-by: Rick Hudson <rlh@golang.org>
2015-03-23 21:07:33 -04:00
|
|
|
go gcBgMarkWorker(p)
|
|
|
|
|
notetsleepg(&work.bgMarkReady, -1)
|
|
|
|
|
noteclear(&work.bgMarkReady)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// gcBgMarkPrepare sets up state for background marking.
|
|
|
|
|
// Mutator assists must not yet be enabled.
|
|
|
|
|
func gcBgMarkPrepare() {
|
|
|
|
|
// Background marking will stop when the work queues are empty
|
|
|
|
|
// and there are no more workers (note that, since this is
|
|
|
|
|
// concurrent, this may be a transient state, but mark
|
|
|
|
|
// termination will clean it up). Between background workers
|
|
|
|
|
// and assists, we don't really know how many workers there
|
|
|
|
|
// will be, so we pretend to have an arbitrarily large number
|
|
|
|
|
// of workers, almost all of which are "waiting". While a
|
|
|
|
|
// worker is working it decrements nwait. If nproc == nwait,
|
|
|
|
|
// there are no workers.
|
|
|
|
|
work.nproc = ^uint32(0)
|
|
|
|
|
work.nwait = ^uint32(0)
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-19 22:45:37 -05:00
|
|
|
func gcBgMarkWorker(_p_ *p) {
|
runtime: never pass stack pointers to gopark
gopark calls the unlock function after setting the G to _Gwaiting.
This means it's generally unsafe to access the G's stack from the
unlock function because the G may start running on another P. Once we
start shrinking stacks concurrently, a stack shrink could also move
the stack the moment after it enters _Gwaiting and before the unlock
function is called.
Document this restriction and fix the two places where we currently
violate it.
This is unlikely to be a problem in practice for these two places
right now, but they're already skating on thin ice. For example, the
following sequence could in principle cause corruption, deadlock, or a
panic in the select code:
On M1/P1:
1. G1 selects on channels A and B.
2. selectgoImpl calls gopark.
3. gopark puts G1 in _Gwaiting.
4. gopark calls selparkcommit.
5. selparkcommit releases the lock on channel A.
On M2/P2:
6. G2 sends to channel A.
7. The send puts G1 in _Grunnable and puts it on P2's run queue.
8. The scheduler runs, selects G1, puts it in _Grunning, and resumes G1.
9. On G1, the sellock immediately following the gopark gets called.
10. sellock grows and moves the stack.
On M1/P1:
11. selparkcommit continues to scan the lock order for the next
channel to unlock, but it's now reading from a freed (and possibly
reused) stack.
This shouldn't happen in practice because step 10 isn't the first call
to sellock, so the stack should already be big enough. However, once
we start shrinking stacks concurrently, this reasoning won't work any
more.
For #12967.
Change-Id: I3660c5be37e5be9f87433cb8141bdfdf37fadc4c
Reviewed-on: https://go-review.googlesource.com/20038
Reviewed-by: Rick Hudson <rlh@golang.org>
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2016-02-26 10:50:54 -05:00
|
|
|
gp := getg()
|
|
|
|
|
|
2016-01-19 22:45:37 -05:00
|
|
|
type parkInfo struct {
|
runtime: never pass stack pointers to gopark
gopark calls the unlock function after setting the G to _Gwaiting.
This means it's generally unsafe to access the G's stack from the
unlock function because the G may start running on another P. Once we
start shrinking stacks concurrently, a stack shrink could also move
the stack the moment after it enters _Gwaiting and before the unlock
function is called.
Document this restriction and fix the two places where we currently
violate it.
This is unlikely to be a problem in practice for these two places
right now, but they're already skating on thin ice. For example, the
following sequence could in principle cause corruption, deadlock, or a
panic in the select code:
On M1/P1:
1. G1 selects on channels A and B.
2. selectgoImpl calls gopark.
3. gopark puts G1 in _Gwaiting.
4. gopark calls selparkcommit.
5. selparkcommit releases the lock on channel A.
On M2/P2:
6. G2 sends to channel A.
7. The send puts G1 in _Grunnable and puts it on P2's run queue.
8. The scheduler runs, selects G1, puts it in _Grunning, and resumes G1.
9. On G1, the sellock immediately following the gopark gets called.
10. sellock grows and moves the stack.
On M1/P1:
11. selparkcommit continues to scan the lock order for the next
channel to unlock, but it's now reading from a freed (and possibly
reused) stack.
This shouldn't happen in practice because step 10 isn't the first call
to sellock, so the stack should already be big enough. However, once
we start shrinking stacks concurrently, this reasoning won't work any
more.
For #12967.
Change-Id: I3660c5be37e5be9f87433cb8141bdfdf37fadc4c
Reviewed-on: https://go-review.googlesource.com/20038
Reviewed-by: Rick Hudson <rlh@golang.org>
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2016-02-26 10:50:54 -05:00
|
|
|
m muintptr // Release this m on park.
|
|
|
|
|
attach puintptr // If non-nil, attach to this p on park.
|
2016-01-19 22:45:37 -05:00
|
|
|
}
|
runtime: never pass stack pointers to gopark
gopark calls the unlock function after setting the G to _Gwaiting.
This means it's generally unsafe to access the G's stack from the
unlock function because the G may start running on another P. Once we
start shrinking stacks concurrently, a stack shrink could also move
the stack the moment after it enters _Gwaiting and before the unlock
function is called.
Document this restriction and fix the two places where we currently
violate it.
This is unlikely to be a problem in practice for these two places
right now, but they're already skating on thin ice. For example, the
following sequence could in principle cause corruption, deadlock, or a
panic in the select code:
On M1/P1:
1. G1 selects on channels A and B.
2. selectgoImpl calls gopark.
3. gopark puts G1 in _Gwaiting.
4. gopark calls selparkcommit.
5. selparkcommit releases the lock on channel A.
On M2/P2:
6. G2 sends to channel A.
7. The send puts G1 in _Grunnable and puts it on P2's run queue.
8. The scheduler runs, selects G1, puts it in _Grunning, and resumes G1.
9. On G1, the sellock immediately following the gopark gets called.
10. sellock grows and moves the stack.
On M1/P1:
11. selparkcommit continues to scan the lock order for the next
channel to unlock, but it's now reading from a freed (and possibly
reused) stack.
This shouldn't happen in practice because step 10 isn't the first call
to sellock, so the stack should already be big enough. However, once
we start shrinking stacks concurrently, this reasoning won't work any
more.
For #12967.
Change-Id: I3660c5be37e5be9f87433cb8141bdfdf37fadc4c
Reviewed-on: https://go-review.googlesource.com/20038
Reviewed-by: Rick Hudson <rlh@golang.org>
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2016-02-26 10:50:54 -05:00
|
|
|
// We pass park to a gopark unlock function, so it can't be on
|
|
|
|
|
// the stack (see gopark). Prevent deadlock from recursively
|
|
|
|
|
// starting GC by disabling preemption.
|
|
|
|
|
gp.m.preemptoff = "GC worker init"
|
|
|
|
|
park := new(parkInfo)
|
|
|
|
|
gp.m.preemptoff = ""
|
|
|
|
|
|
|
|
|
|
park.m.set(acquirem())
|
|
|
|
|
park.attach.set(_p_)
|
2016-01-19 22:45:37 -05:00
|
|
|
// Inform gcBgMarkStartWorkers that this worker is ready.
|
runtime: multi-threaded, utilization-scheduled background mark
Currently, the concurrent mark phase is performed by the main GC
goroutine. Prior to the previous commit enabling preemption, this
caused marking to always consume 1/GOMAXPROCS of the available CPU
time. If GOMAXPROCS=1, this meant background GC would consume 100% of
the CPU (effectively a STW). If GOMAXPROCS>4, background GC would use
less than the goal of 25%. If GOMAXPROCS=4, background GC would use
the goal 25%, but if the mutator wasn't using the remaining 75%,
background marking wouldn't take advantage of the idle time. Enabling
preemption in the previous commit made GC miss CPU targets in
completely different ways, but set us up to bring everything back in
line.
This change replaces the fixed GC goroutine with per-P background mark
goroutines. Once started, these goroutines don't go in the standard
run queues; instead, they are scheduled specially such that the time
spent in mutator assists and the background mark goroutines totals 25%
of the CPU time available to the program. Furthermore, this lets
background marking take advantage of idle Ps, which significantly
boosts GC performance for applications that under-utilize the CPU.
This requires also changing how time is reported for gctrace, so this
change splits the concurrent mark CPU time into assist/background/idle
scanning.
This also requires increasing the size of the StackRecord slice used
in a GoroutineProfile test.
Change-Id: I0936ff907d2cee6cb687a208f2df47e8988e3157
Reviewed-on: https://go-review.googlesource.com/8850
Reviewed-by: Rick Hudson <rlh@golang.org>
2015-03-23 21:07:33 -04:00
|
|
|
// After this point, the background mark worker is scheduled
|
|
|
|
|
// cooperatively by gcController.findRunnable. Hence, it must
|
|
|
|
|
// never be preempted, as this would put it into _Grunnable
|
|
|
|
|
// and put it on a run queue. Instead, when the preempt flag
|
|
|
|
|
// is set, this puts itself into _Gwaiting to be woken up by
|
|
|
|
|
// gcController.findRunnable at the appropriate time.
|
|
|
|
|
notewakeup(&work.bgMarkReady)
|
2015-10-26 11:27:37 -04:00
|
|
|
|
runtime: multi-threaded, utilization-scheduled background mark
Currently, the concurrent mark phase is performed by the main GC
goroutine. Prior to the previous commit enabling preemption, this
caused marking to always consume 1/GOMAXPROCS of the available CPU
time. If GOMAXPROCS=1, this meant background GC would consume 100% of
the CPU (effectively a STW). If GOMAXPROCS>4, background GC would use
less than the goal of 25%. If GOMAXPROCS=4, background GC would use
the goal 25%, but if the mutator wasn't using the remaining 75%,
background marking wouldn't take advantage of the idle time. Enabling
preemption in the previous commit made GC miss CPU targets in
completely different ways, but set us up to bring everything back in
line.
This change replaces the fixed GC goroutine with per-P background mark
goroutines. Once started, these goroutines don't go in the standard
run queues; instead, they are scheduled specially such that the time
spent in mutator assists and the background mark goroutines totals 25%
of the CPU time available to the program. Furthermore, this lets
background marking take advantage of idle Ps, which significantly
boosts GC performance for applications that under-utilize the CPU.
This requires also changing how time is reported for gctrace, so this
change splits the concurrent mark CPU time into assist/background/idle
scanning.
This also requires increasing the size of the StackRecord slice used
in a GoroutineProfile test.
Change-Id: I0936ff907d2cee6cb687a208f2df47e8988e3157
Reviewed-on: https://go-review.googlesource.com/8850
Reviewed-by: Rick Hudson <rlh@golang.org>
2015-03-23 21:07:33 -04:00
|
|
|
for {
|
2016-05-24 14:12:35 -07:00
|
|
|
// Go to sleep until woken by gcController.findRunnable.
|
runtime: multi-threaded, utilization-scheduled background mark
Currently, the concurrent mark phase is performed by the main GC
goroutine. Prior to the previous commit enabling preemption, this
caused marking to always consume 1/GOMAXPROCS of the available CPU
time. If GOMAXPROCS=1, this meant background GC would consume 100% of
the CPU (effectively a STW). If GOMAXPROCS>4, background GC would use
less than the goal of 25%. If GOMAXPROCS=4, background GC would use
the goal 25%, but if the mutator wasn't using the remaining 75%,
background marking wouldn't take advantage of the idle time. Enabling
preemption in the previous commit made GC miss CPU targets in
completely different ways, but set us up to bring everything back in
line.
This change replaces the fixed GC goroutine with per-P background mark
goroutines. Once started, these goroutines don't go in the standard
run queues; instead, they are scheduled specially such that the time
spent in mutator assists and the background mark goroutines totals 25%
of the CPU time available to the program. Furthermore, this lets
background marking take advantage of idle Ps, which significantly
boosts GC performance for applications that under-utilize the CPU.
This requires also changing how time is reported for gctrace, so this
change splits the concurrent mark CPU time into assist/background/idle
scanning.
This also requires increasing the size of the StackRecord slice used
in a GoroutineProfile test.
Change-Id: I0936ff907d2cee6cb687a208f2df47e8988e3157
Reviewed-on: https://go-review.googlesource.com/8850
Reviewed-by: Rick Hudson <rlh@golang.org>
2015-03-23 21:07:33 -04:00
|
|
|
// We can't releasem yet since even the call to gopark
|
|
|
|
|
// may be preempted.
|
2016-01-19 22:45:37 -05:00
|
|
|
gopark(func(g *g, parkp unsafe.Pointer) bool {
|
|
|
|
|
park := (*parkInfo)(parkp)
|
|
|
|
|
|
|
|
|
|
// The worker G is no longer running, so it's
|
|
|
|
|
// now safe to allow preemption.
|
runtime: never pass stack pointers to gopark
gopark calls the unlock function after setting the G to _Gwaiting.
This means it's generally unsafe to access the G's stack from the
unlock function because the G may start running on another P. Once we
start shrinking stacks concurrently, a stack shrink could also move
the stack the moment after it enters _Gwaiting and before the unlock
function is called.
Document this restriction and fix the two places where we currently
violate it.
This is unlikely to be a problem in practice for these two places
right now, but they're already skating on thin ice. For example, the
following sequence could in principle cause corruption, deadlock, or a
panic in the select code:
On M1/P1:
1. G1 selects on channels A and B.
2. selectgoImpl calls gopark.
3. gopark puts G1 in _Gwaiting.
4. gopark calls selparkcommit.
5. selparkcommit releases the lock on channel A.
On M2/P2:
6. G2 sends to channel A.
7. The send puts G1 in _Grunnable and puts it on P2's run queue.
8. The scheduler runs, selects G1, puts it in _Grunning, and resumes G1.
9. On G1, the sellock immediately following the gopark gets called.
10. sellock grows and moves the stack.
On M1/P1:
11. selparkcommit continues to scan the lock order for the next
channel to unlock, but it's now reading from a freed (and possibly
reused) stack.
This shouldn't happen in practice because step 10 isn't the first call
to sellock, so the stack should already be big enough. However, once
we start shrinking stacks concurrently, this reasoning won't work any
more.
For #12967.
Change-Id: I3660c5be37e5be9f87433cb8141bdfdf37fadc4c
Reviewed-on: https://go-review.googlesource.com/20038
Reviewed-by: Rick Hudson <rlh@golang.org>
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2016-02-26 10:50:54 -05:00
|
|
|
releasem(park.m.ptr())
|
2016-01-19 22:45:37 -05:00
|
|
|
|
|
|
|
|
// If the worker isn't attached to its P,
|
|
|
|
|
// attach now. During initialization and after
|
|
|
|
|
// a phase change, the worker may have been
|
|
|
|
|
// running on a different P. As soon as we
|
|
|
|
|
// attach, the owner P may schedule the
|
|
|
|
|
// worker, so this must be done after the G is
|
|
|
|
|
// stopped.
|
runtime: never pass stack pointers to gopark
gopark calls the unlock function after setting the G to _Gwaiting.
This means it's generally unsafe to access the G's stack from the
unlock function because the G may start running on another P. Once we
start shrinking stacks concurrently, a stack shrink could also move
the stack the moment after it enters _Gwaiting and before the unlock
function is called.
Document this restriction and fix the two places where we currently
violate it.
This is unlikely to be a problem in practice for these two places
right now, but they're already skating on thin ice. For example, the
following sequence could in principle cause corruption, deadlock, or a
panic in the select code:
On M1/P1:
1. G1 selects on channels A and B.
2. selectgoImpl calls gopark.
3. gopark puts G1 in _Gwaiting.
4. gopark calls selparkcommit.
5. selparkcommit releases the lock on channel A.
On M2/P2:
6. G2 sends to channel A.
7. The send puts G1 in _Grunnable and puts it on P2's run queue.
8. The scheduler runs, selects G1, puts it in _Grunning, and resumes G1.
9. On G1, the sellock immediately following the gopark gets called.
10. sellock grows and moves the stack.
On M1/P1:
11. selparkcommit continues to scan the lock order for the next
channel to unlock, but it's now reading from a freed (and possibly
reused) stack.
This shouldn't happen in practice because step 10 isn't the first call
to sellock, so the stack should already be big enough. However, once
we start shrinking stacks concurrently, this reasoning won't work any
more.
For #12967.
Change-Id: I3660c5be37e5be9f87433cb8141bdfdf37fadc4c
Reviewed-on: https://go-review.googlesource.com/20038
Reviewed-by: Rick Hudson <rlh@golang.org>
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2016-02-26 10:50:54 -05:00
|
|
|
if park.attach != 0 {
|
|
|
|
|
p := park.attach.ptr()
|
|
|
|
|
park.attach.set(nil)
|
2016-01-19 22:45:37 -05:00
|
|
|
// cas the worker because we may be
|
|
|
|
|
// racing with a new worker starting
|
|
|
|
|
// on this P.
|
2016-01-26 14:44:58 -05:00
|
|
|
if !p.gcBgMarkWorker.cas(0, guintptr(unsafe.Pointer(g))) {
|
2016-01-19 22:45:37 -05:00
|
|
|
// The P got a new worker.
|
|
|
|
|
// Exit this worker.
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
}
|
runtime: multi-threaded, utilization-scheduled background mark
Currently, the concurrent mark phase is performed by the main GC
goroutine. Prior to the previous commit enabling preemption, this
caused marking to always consume 1/GOMAXPROCS of the available CPU
time. If GOMAXPROCS=1, this meant background GC would consume 100% of
the CPU (effectively a STW). If GOMAXPROCS>4, background GC would use
less than the goal of 25%. If GOMAXPROCS=4, background GC would use
the goal 25%, but if the mutator wasn't using the remaining 75%,
background marking wouldn't take advantage of the idle time. Enabling
preemption in the previous commit made GC miss CPU targets in
completely different ways, but set us up to bring everything back in
line.
This change replaces the fixed GC goroutine with per-P background mark
goroutines. Once started, these goroutines don't go in the standard
run queues; instead, they are scheduled specially such that the time
spent in mutator assists and the background mark goroutines totals 25%
of the CPU time available to the program. Furthermore, this lets
background marking take advantage of idle Ps, which significantly
boosts GC performance for applications that under-utilize the CPU.
This requires also changing how time is reported for gctrace, so this
change splits the concurrent mark CPU time into assist/background/idle
scanning.
This also requires increasing the size of the StackRecord slice used
in a GoroutineProfile test.
Change-Id: I0936ff907d2cee6cb687a208f2df47e8988e3157
Reviewed-on: https://go-review.googlesource.com/8850
Reviewed-by: Rick Hudson <rlh@golang.org>
2015-03-23 21:07:33 -04:00
|
|
|
return true
|
runtime: never pass stack pointers to gopark
gopark calls the unlock function after setting the G to _Gwaiting.
This means it's generally unsafe to access the G's stack from the
unlock function because the G may start running on another P. Once we
start shrinking stacks concurrently, a stack shrink could also move
the stack the moment after it enters _Gwaiting and before the unlock
function is called.
Document this restriction and fix the two places where we currently
violate it.
This is unlikely to be a problem in practice for these two places
right now, but they're already skating on thin ice. For example, the
following sequence could in principle cause corruption, deadlock, or a
panic in the select code:
On M1/P1:
1. G1 selects on channels A and B.
2. selectgoImpl calls gopark.
3. gopark puts G1 in _Gwaiting.
4. gopark calls selparkcommit.
5. selparkcommit releases the lock on channel A.
On M2/P2:
6. G2 sends to channel A.
7. The send puts G1 in _Grunnable and puts it on P2's run queue.
8. The scheduler runs, selects G1, puts it in _Grunning, and resumes G1.
9. On G1, the sellock immediately following the gopark gets called.
10. sellock grows and moves the stack.
On M1/P1:
11. selparkcommit continues to scan the lock order for the next
channel to unlock, but it's now reading from a freed (and possibly
reused) stack.
This shouldn't happen in practice because step 10 isn't the first call
to sellock, so the stack should already be big enough. However, once
we start shrinking stacks concurrently, this reasoning won't work any
more.
For #12967.
Change-Id: I3660c5be37e5be9f87433cb8141bdfdf37fadc4c
Reviewed-on: https://go-review.googlesource.com/20038
Reviewed-by: Rick Hudson <rlh@golang.org>
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2016-02-26 10:50:54 -05:00
|
|
|
}, unsafe.Pointer(park), "GC worker (idle)", traceEvGoBlock, 0)
|
runtime: multi-threaded, utilization-scheduled background mark
Currently, the concurrent mark phase is performed by the main GC
goroutine. Prior to the previous commit enabling preemption, this
caused marking to always consume 1/GOMAXPROCS of the available CPU
time. If GOMAXPROCS=1, this meant background GC would consume 100% of
the CPU (effectively a STW). If GOMAXPROCS>4, background GC would use
less than the goal of 25%. If GOMAXPROCS=4, background GC would use
the goal 25%, but if the mutator wasn't using the remaining 75%,
background marking wouldn't take advantage of the idle time. Enabling
preemption in the previous commit made GC miss CPU targets in
completely different ways, but set us up to bring everything back in
line.
This change replaces the fixed GC goroutine with per-P background mark
goroutines. Once started, these goroutines don't go in the standard
run queues; instead, they are scheduled specially such that the time
spent in mutator assists and the background mark goroutines totals 25%
of the CPU time available to the program. Furthermore, this lets
background marking take advantage of idle Ps, which significantly
boosts GC performance for applications that under-utilize the CPU.
This requires also changing how time is reported for gctrace, so this
change splits the concurrent mark CPU time into assist/background/idle
scanning.
This also requires increasing the size of the StackRecord slice used
in a GoroutineProfile test.
Change-Id: I0936ff907d2cee6cb687a208f2df47e8988e3157
Reviewed-on: https://go-review.googlesource.com/8850
Reviewed-by: Rick Hudson <rlh@golang.org>
2015-03-23 21:07:33 -04:00
|
|
|
|
|
|
|
|
// Loop until the P dies and disassociates this
|
2016-01-19 22:45:37 -05:00
|
|
|
// worker (the P may later be reused, in which case
|
|
|
|
|
// it will get a new worker) or we failed to associate.
|
2016-01-26 14:44:58 -05:00
|
|
|
if _p_.gcBgMarkWorker.ptr() != gp {
|
runtime: multi-threaded, utilization-scheduled background mark
Currently, the concurrent mark phase is performed by the main GC
goroutine. Prior to the previous commit enabling preemption, this
caused marking to always consume 1/GOMAXPROCS of the available CPU
time. If GOMAXPROCS=1, this meant background GC would consume 100% of
the CPU (effectively a STW). If GOMAXPROCS>4, background GC would use
less than the goal of 25%. If GOMAXPROCS=4, background GC would use
the goal 25%, but if the mutator wasn't using the remaining 75%,
background marking wouldn't take advantage of the idle time. Enabling
preemption in the previous commit made GC miss CPU targets in
completely different ways, but set us up to bring everything back in
line.
This change replaces the fixed GC goroutine with per-P background mark
goroutines. Once started, these goroutines don't go in the standard
run queues; instead, they are scheduled specially such that the time
spent in mutator assists and the background mark goroutines totals 25%
of the CPU time available to the program. Furthermore, this lets
background marking take advantage of idle Ps, which significantly
boosts GC performance for applications that under-utilize the CPU.
This requires also changing how time is reported for gctrace, so this
change splits the concurrent mark CPU time into assist/background/idle
scanning.
This also requires increasing the size of the StackRecord slice used
in a GoroutineProfile test.
Change-Id: I0936ff907d2cee6cb687a208f2df47e8988e3157
Reviewed-on: https://go-review.googlesource.com/8850
Reviewed-by: Rick Hudson <rlh@golang.org>
2015-03-23 21:07:33 -04:00
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Disable preemption so we can use the gcw. If the
|
|
|
|
|
// scheduler wants to preempt us, we'll stop draining,
|
|
|
|
|
// dispose the gcw, and then preempt.
|
runtime: never pass stack pointers to gopark
gopark calls the unlock function after setting the G to _Gwaiting.
This means it's generally unsafe to access the G's stack from the
unlock function because the G may start running on another P. Once we
start shrinking stacks concurrently, a stack shrink could also move
the stack the moment after it enters _Gwaiting and before the unlock
function is called.
Document this restriction and fix the two places where we currently
violate it.
This is unlikely to be a problem in practice for these two places
right now, but they're already skating on thin ice. For example, the
following sequence could in principle cause corruption, deadlock, or a
panic in the select code:
On M1/P1:
1. G1 selects on channels A and B.
2. selectgoImpl calls gopark.
3. gopark puts G1 in _Gwaiting.
4. gopark calls selparkcommit.
5. selparkcommit releases the lock on channel A.
On M2/P2:
6. G2 sends to channel A.
7. The send puts G1 in _Grunnable and puts it on P2's run queue.
8. The scheduler runs, selects G1, puts it in _Grunning, and resumes G1.
9. On G1, the sellock immediately following the gopark gets called.
10. sellock grows and moves the stack.
On M1/P1:
11. selparkcommit continues to scan the lock order for the next
channel to unlock, but it's now reading from a freed (and possibly
reused) stack.
This shouldn't happen in practice because step 10 isn't the first call
to sellock, so the stack should already be big enough. However, once
we start shrinking stacks concurrently, this reasoning won't work any
more.
For #12967.
Change-Id: I3660c5be37e5be9f87433cb8141bdfdf37fadc4c
Reviewed-on: https://go-review.googlesource.com/20038
Reviewed-by: Rick Hudson <rlh@golang.org>
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2016-02-26 10:50:54 -05:00
|
|
|
park.m.set(acquirem())
|
runtime: multi-threaded, utilization-scheduled background mark
Currently, the concurrent mark phase is performed by the main GC
goroutine. Prior to the previous commit enabling preemption, this
caused marking to always consume 1/GOMAXPROCS of the available CPU
time. If GOMAXPROCS=1, this meant background GC would consume 100% of
the CPU (effectively a STW). If GOMAXPROCS>4, background GC would use
less than the goal of 25%. If GOMAXPROCS=4, background GC would use
the goal 25%, but if the mutator wasn't using the remaining 75%,
background marking wouldn't take advantage of the idle time. Enabling
preemption in the previous commit made GC miss CPU targets in
completely different ways, but set us up to bring everything back in
line.
This change replaces the fixed GC goroutine with per-P background mark
goroutines. Once started, these goroutines don't go in the standard
run queues; instead, they are scheduled specially such that the time
spent in mutator assists and the background mark goroutines totals 25%
of the CPU time available to the program. Furthermore, this lets
background marking take advantage of idle Ps, which significantly
boosts GC performance for applications that under-utilize the CPU.
This requires also changing how time is reported for gctrace, so this
change splits the concurrent mark CPU time into assist/background/idle
scanning.
This also requires increasing the size of the StackRecord slice used
in a GoroutineProfile test.
Change-Id: I0936ff907d2cee6cb687a208f2df47e8988e3157
Reviewed-on: https://go-review.googlesource.com/8850
Reviewed-by: Rick Hudson <rlh@golang.org>
2015-03-23 21:07:33 -04:00
|
|
|
|
2015-03-27 17:01:53 -04:00
|
|
|
if gcBlackenEnabled == 0 {
|
|
|
|
|
throw("gcBgMarkWorker: blackening not enabled")
|
|
|
|
|
}
|
|
|
|
|
|
runtime: multi-threaded, utilization-scheduled background mark
Currently, the concurrent mark phase is performed by the main GC
goroutine. Prior to the previous commit enabling preemption, this
caused marking to always consume 1/GOMAXPROCS of the available CPU
time. If GOMAXPROCS=1, this meant background GC would consume 100% of
the CPU (effectively a STW). If GOMAXPROCS>4, background GC would use
less than the goal of 25%. If GOMAXPROCS=4, background GC would use
the goal 25%, but if the mutator wasn't using the remaining 75%,
background marking wouldn't take advantage of the idle time. Enabling
preemption in the previous commit made GC miss CPU targets in
completely different ways, but set us up to bring everything back in
line.
This change replaces the fixed GC goroutine with per-P background mark
goroutines. Once started, these goroutines don't go in the standard
run queues; instead, they are scheduled specially such that the time
spent in mutator assists and the background mark goroutines totals 25%
of the CPU time available to the program. Furthermore, this lets
background marking take advantage of idle Ps, which significantly
boosts GC performance for applications that under-utilize the CPU.
This requires also changing how time is reported for gctrace, so this
change splits the concurrent mark CPU time into assist/background/idle
scanning.
This also requires increasing the size of the StackRecord slice used
in a GoroutineProfile test.
Change-Id: I0936ff907d2cee6cb687a208f2df47e8988e3157
Reviewed-on: https://go-review.googlesource.com/8850
Reviewed-by: Rick Hudson <rlh@golang.org>
2015-03-23 21:07:33 -04:00
|
|
|
startTime := nanotime()
|
2017-10-04 16:15:35 -04:00
|
|
|
_p_.gcMarkWorkerStartTime = startTime
|
runtime: multi-threaded, utilization-scheduled background mark
Currently, the concurrent mark phase is performed by the main GC
goroutine. Prior to the previous commit enabling preemption, this
caused marking to always consume 1/GOMAXPROCS of the available CPU
time. If GOMAXPROCS=1, this meant background GC would consume 100% of
the CPU (effectively a STW). If GOMAXPROCS>4, background GC would use
less than the goal of 25%. If GOMAXPROCS=4, background GC would use
the goal 25%, but if the mutator wasn't using the remaining 75%,
background marking wouldn't take advantage of the idle time. Enabling
preemption in the previous commit made GC miss CPU targets in
completely different ways, but set us up to bring everything back in
line.
This change replaces the fixed GC goroutine with per-P background mark
goroutines. Once started, these goroutines don't go in the standard
run queues; instead, they are scheduled specially such that the time
spent in mutator assists and the background mark goroutines totals 25%
of the CPU time available to the program. Furthermore, this lets
background marking take advantage of idle Ps, which significantly
boosts GC performance for applications that under-utilize the CPU.
This requires also changing how time is reported for gctrace, so this
change splits the concurrent mark CPU time into assist/background/idle
scanning.
This also requires increasing the size of the StackRecord slice used
in a GoroutineProfile test.
Change-Id: I0936ff907d2cee6cb687a208f2df47e8988e3157
Reviewed-on: https://go-review.googlesource.com/8850
Reviewed-by: Rick Hudson <rlh@golang.org>
2015-03-23 21:07:33 -04:00
|
|
|
|
2015-11-02 14:09:24 -05:00
|
|
|
decnwait := atomic.Xadd(&work.nwait, -1)
|
2015-06-01 18:16:03 -04:00
|
|
|
if decnwait == work.nproc {
|
|
|
|
|
println("runtime: work.nwait=", decnwait, "work.nproc=", work.nproc)
|
|
|
|
|
throw("work.nwait was > work.nproc")
|
|
|
|
|
}
|
runtime: multi-threaded, utilization-scheduled background mark
Currently, the concurrent mark phase is performed by the main GC
goroutine. Prior to the previous commit enabling preemption, this
caused marking to always consume 1/GOMAXPROCS of the available CPU
time. If GOMAXPROCS=1, this meant background GC would consume 100% of
the CPU (effectively a STW). If GOMAXPROCS>4, background GC would use
less than the goal of 25%. If GOMAXPROCS=4, background GC would use
the goal 25%, but if the mutator wasn't using the remaining 75%,
background marking wouldn't take advantage of the idle time. Enabling
preemption in the previous commit made GC miss CPU targets in
completely different ways, but set us up to bring everything back in
line.
This change replaces the fixed GC goroutine with per-P background mark
goroutines. Once started, these goroutines don't go in the standard
run queues; instead, they are scheduled specially such that the time
spent in mutator assists and the background mark goroutines totals 25%
of the CPU time available to the program. Furthermore, this lets
background marking take advantage of idle Ps, which significantly
boosts GC performance for applications that under-utilize the CPU.
This requires also changing how time is reported for gctrace, so this
change splits the concurrent mark CPU time into assist/background/idle
scanning.
This also requires increasing the size of the StackRecord slice used
in a GoroutineProfile test.
Change-Id: I0936ff907d2cee6cb687a208f2df47e8988e3157
Reviewed-on: https://go-review.googlesource.com/8850
Reviewed-by: Rick Hudson <rlh@golang.org>
2015-03-23 21:07:33 -04:00
|
|
|
|
runtime: scan mark worker stacks like normal
Currently, markroot delays scanning mark worker stacks until mark
termination by putting the mark worker G directly on the rescan list
when it encounters one during the mark phase. Without this, since mark
workers are non-preemptible, two mark workers that attempt to scan
each other's stacks can deadlock.
However, this is annoyingly asymmetric and causes some real problems.
First, markroot does not own the G at that point, so it's not
technically safe to add it to the rescan list. I haven't been able to
find a specific problem this could cause, but I suspect it's the root
cause of issue #17099. Second, this will interfere with the hybrid
barrier, since there is no stack rescanning during mark termination
with the hybrid barrier.
This commit switches to a different approach. We move the mark
worker's call to gcDrain to the system stack and set the mark worker's
status to _Gwaiting for the duration of the drain to indicate that
it's preemptible. This lets another mark worker scan its G stack while
the drain is running on the system stack. We don't return to the G
stack until we can switch back to _Grunning, which ensures we don't
race with a stack scan. This lets us eliminate the special case for
mark worker stack scans and scan them just like any other goroutine.
The only subtlety to this approach is that we have to disable stack
shrinking for mark workers; they could be referring to captured
variables from the G stack, so it's not safe to move their stacks.
Updates #17099 and #17503.
Change-Id: Ia5213949ec470af63e24dfce01df357c12adbbea
Reviewed-on: https://go-review.googlesource.com/31820
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rick Hudson <rlh@golang.org>
2016-10-24 14:20:07 -04:00
|
|
|
systemstack(func() {
|
|
|
|
|
// Mark our goroutine preemptible so its stack
|
|
|
|
|
// can be scanned. This lets two mark workers
|
|
|
|
|
// scan each other (otherwise, they would
|
|
|
|
|
// deadlock). We must not modify anything on
|
|
|
|
|
// the G stack. However, stack shrinking is
|
|
|
|
|
// disabled for mark workers, so it is safe to
|
|
|
|
|
// read from the G stack.
|
|
|
|
|
casgstatus(gp, _Grunning, _Gwaiting)
|
|
|
|
|
switch _p_.gcMarkWorkerMode {
|
|
|
|
|
default:
|
|
|
|
|
throw("gcBgMarkWorker: unexpected gcMarkWorkerMode")
|
|
|
|
|
case gcMarkWorkerDedicatedMode:
|
2017-06-23 17:54:39 -04:00
|
|
|
gcDrain(&_p_.gcw, gcDrainUntilPreempt|gcDrainFlushBgCredit)
|
|
|
|
|
if gp.preempt {
|
|
|
|
|
// We were preempted. This is
|
|
|
|
|
// a useful signal to kick
|
|
|
|
|
// everything out of the run
|
|
|
|
|
// queue so it can run
|
|
|
|
|
// somewhere else.
|
|
|
|
|
lock(&sched.lock)
|
|
|
|
|
for {
|
|
|
|
|
gp, _ := runqget(_p_)
|
|
|
|
|
if gp == nil {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
globrunqput(gp)
|
|
|
|
|
}
|
|
|
|
|
unlock(&sched.lock)
|
|
|
|
|
}
|
|
|
|
|
// Go back to draining, this time
|
|
|
|
|
// without preemption.
|
runtime: scan mark worker stacks like normal
Currently, markroot delays scanning mark worker stacks until mark
termination by putting the mark worker G directly on the rescan list
when it encounters one during the mark phase. Without this, since mark
workers are non-preemptible, two mark workers that attempt to scan
each other's stacks can deadlock.
However, this is annoyingly asymmetric and causes some real problems.
First, markroot does not own the G at that point, so it's not
technically safe to add it to the rescan list. I haven't been able to
find a specific problem this could cause, but I suspect it's the root
cause of issue #17099. Second, this will interfere with the hybrid
barrier, since there is no stack rescanning during mark termination
with the hybrid barrier.
This commit switches to a different approach. We move the mark
worker's call to gcDrain to the system stack and set the mark worker's
status to _Gwaiting for the duration of the drain to indicate that
it's preemptible. This lets another mark worker scan its G stack while
the drain is running on the system stack. We don't return to the G
stack until we can switch back to _Grunning, which ensures we don't
race with a stack scan. This lets us eliminate the special case for
mark worker stack scans and scan them just like any other goroutine.
The only subtlety to this approach is that we have to disable stack
shrinking for mark workers; they could be referring to captured
variables from the G stack, so it's not safe to move their stacks.
Updates #17099 and #17503.
Change-Id: Ia5213949ec470af63e24dfce01df357c12adbbea
Reviewed-on: https://go-review.googlesource.com/31820
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rick Hudson <rlh@golang.org>
2016-10-24 14:20:07 -04:00
|
|
|
gcDrain(&_p_.gcw, gcDrainNoBlock|gcDrainFlushBgCredit)
|
2016-10-30 20:20:17 -04:00
|
|
|
case gcMarkWorkerFractionalMode:
|
2017-10-04 16:15:35 -04:00
|
|
|
gcDrain(&_p_.gcw, gcDrainFractional|gcDrainUntilPreempt|gcDrainFlushBgCredit)
|
2016-10-30 20:20:17 -04:00
|
|
|
case gcMarkWorkerIdleMode:
|
|
|
|
|
gcDrain(&_p_.gcw, gcDrainIdle|gcDrainUntilPreempt|gcDrainFlushBgCredit)
|
runtime: scan mark worker stacks like normal
Currently, markroot delays scanning mark worker stacks until mark
termination by putting the mark worker G directly on the rescan list
when it encounters one during the mark phase. Without this, since mark
workers are non-preemptible, two mark workers that attempt to scan
each other's stacks can deadlock.
However, this is annoyingly asymmetric and causes some real problems.
First, markroot does not own the G at that point, so it's not
technically safe to add it to the rescan list. I haven't been able to
find a specific problem this could cause, but I suspect it's the root
cause of issue #17099. Second, this will interfere with the hybrid
barrier, since there is no stack rescanning during mark termination
with the hybrid barrier.
This commit switches to a different approach. We move the mark
worker's call to gcDrain to the system stack and set the mark worker's
status to _Gwaiting for the duration of the drain to indicate that
it's preemptible. This lets another mark worker scan its G stack while
the drain is running on the system stack. We don't return to the G
stack until we can switch back to _Grunning, which ensures we don't
race with a stack scan. This lets us eliminate the special case for
mark worker stack scans and scan them just like any other goroutine.
The only subtlety to this approach is that we have to disable stack
shrinking for mark workers; they could be referring to captured
variables from the G stack, so it's not safe to move their stacks.
Updates #17099 and #17503.
Change-Id: Ia5213949ec470af63e24dfce01df357c12adbbea
Reviewed-on: https://go-review.googlesource.com/31820
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rick Hudson <rlh@golang.org>
2016-10-24 14:20:07 -04:00
|
|
|
}
|
|
|
|
|
casgstatus(gp, _Gwaiting, _Grunning)
|
|
|
|
|
})
|
2015-07-24 16:38:19 -04:00
|
|
|
|
runtime: eliminate getfull barrier from concurrent mark
Currently dedicated mark workers participate in the getfull barrier
during concurrent mark. However, the getfull barrier wasn't designed
for concurrent work and this causes no end of headaches.
In the concurrent setting, participants come and go. This makes mark
completion susceptible to live-lock: since dedicated workers are only
periodically polling for completion, it's possible for the program to
be in some transient worker each time one of the dedicated workers
wakes up to check if it can exit the getfull barrier. It also
complicates reasoning about the system because dedicated workers
participate directly in the getfull barrier, but transient workers
must instead use trygetfull because they have exit conditions that
aren't captured by getfull (e.g., fractional workers exit when
preempted). The complexity of implementing these exit conditions
contributed to #11677. Furthermore, the getfull barrier is inefficient
because we could be running user code instead of spinning on a P. In
effect, we're dedicating 25% of the CPU to marking even if that means
we have to spin to make that 25%. It also causes issues on Windows
because we can't actually sleep for 100µs (#8687).
Fix this by making dedicated workers no longer participate in the
getfull barrier. Instead, dedicated workers simply return to the
scheduler when they fail to get more work, regardless of what others
workers are doing, and the scheduler only starts new dedicated workers
if there's work available. Everything that needs to be handled by this
barrier is already handled by detection of mark completion.
This makes the system much more symmetric because all workers and
assists now use trygetfull during concurrent mark. It also loosens the
25% CPU target so that we can give some of that 25% back to user code
if there isn't enough work to keep the mark worker busy. And it
eliminates the problematic 100µs sleep on Windows during concurrent
mark (though not during mark termination).
The downside of this is that if we hit a bottleneck in the heap graph
that then expands back out, the system may shut down dedicated workers
and take a while to start them back up. We'll address this in the next
commit.
Updates #12041 and #8687.
No effect on the go1 benchmarks. This slows down the garbage benchmark
by 9%, but we'll more than make it up in the next commit.
name old time/op new time/op delta
XBenchGarbage-12 5.80ms ± 2% 6.32ms ± 4% +9.03% (p=0.000 n=20+20)
Change-Id: I65100a9ba005a8b5cf97940798918672ea9dd09b
Reviewed-on: https://go-review.googlesource.com/16297
Reviewed-by: Rick Hudson <rlh@golang.org>
2015-10-26 16:29:25 -04:00
|
|
|
// If we are nearing the end of mark, dispose
|
|
|
|
|
// of the cache promptly. We must do this
|
|
|
|
|
// before signaling that we're no longer
|
|
|
|
|
// working so that other workers can't observe
|
|
|
|
|
// no workers and no work while we have this
|
|
|
|
|
// cached, and before we compute done.
|
|
|
|
|
if gcBlackenPromptly {
|
2016-01-19 22:45:37 -05:00
|
|
|
_p_.gcw.dispose()
|
runtime: eliminate getfull barrier from concurrent mark
Currently dedicated mark workers participate in the getfull barrier
during concurrent mark. However, the getfull barrier wasn't designed
for concurrent work and this causes no end of headaches.
In the concurrent setting, participants come and go. This makes mark
completion susceptible to live-lock: since dedicated workers are only
periodically polling for completion, it's possible for the program to
be in some transient worker each time one of the dedicated workers
wakes up to check if it can exit the getfull barrier. It also
complicates reasoning about the system because dedicated workers
participate directly in the getfull barrier, but transient workers
must instead use trygetfull because they have exit conditions that
aren't captured by getfull (e.g., fractional workers exit when
preempted). The complexity of implementing these exit conditions
contributed to #11677. Furthermore, the getfull barrier is inefficient
because we could be running user code instead of spinning on a P. In
effect, we're dedicating 25% of the CPU to marking even if that means
we have to spin to make that 25%. It also causes issues on Windows
because we can't actually sleep for 100µs (#8687).
Fix this by making dedicated workers no longer participate in the
getfull barrier. Instead, dedicated workers simply return to the
scheduler when they fail to get more work, regardless of what others
workers are doing, and the scheduler only starts new dedicated workers
if there's work available. Everything that needs to be handled by this
barrier is already handled by detection of mark completion.
This makes the system much more symmetric because all workers and
assists now use trygetfull during concurrent mark. It also loosens the
25% CPU target so that we can give some of that 25% back to user code
if there isn't enough work to keep the mark worker busy. And it
eliminates the problematic 100µs sleep on Windows during concurrent
mark (though not during mark termination).
The downside of this is that if we hit a bottleneck in the heap graph
that then expands back out, the system may shut down dedicated workers
and take a while to start them back up. We'll address this in the next
commit.
Updates #12041 and #8687.
No effect on the go1 benchmarks. This slows down the garbage benchmark
by 9%, but we'll more than make it up in the next commit.
name old time/op new time/op delta
XBenchGarbage-12 5.80ms ± 2% 6.32ms ± 4% +9.03% (p=0.000 n=20+20)
Change-Id: I65100a9ba005a8b5cf97940798918672ea9dd09b
Reviewed-on: https://go-review.googlesource.com/16297
Reviewed-by: Rick Hudson <rlh@golang.org>
2015-10-26 16:29:25 -04:00
|
|
|
}
|
2015-07-24 16:38:19 -04:00
|
|
|
|
2015-10-26 16:48:36 -04:00
|
|
|
// Account for time.
|
|
|
|
|
duration := nanotime() - startTime
|
2016-01-19 22:45:37 -05:00
|
|
|
switch _p_.gcMarkWorkerMode {
|
2015-10-26 16:48:36 -04:00
|
|
|
case gcMarkWorkerDedicatedMode:
|
2015-11-02 14:09:24 -05:00
|
|
|
atomic.Xaddint64(&gcController.dedicatedMarkTime, duration)
|
|
|
|
|
atomic.Xaddint64(&gcController.dedicatedMarkWorkersNeeded, 1)
|
2015-10-26 16:48:36 -04:00
|
|
|
case gcMarkWorkerFractionalMode:
|
2015-11-02 14:09:24 -05:00
|
|
|
atomic.Xaddint64(&gcController.fractionalMarkTime, duration)
|
2017-10-05 12:16:45 -04:00
|
|
|
atomic.Xaddint64(&_p_.gcFractionalMarkTime, duration)
|
2015-10-26 16:48:36 -04:00
|
|
|
case gcMarkWorkerIdleMode:
|
2015-11-02 14:09:24 -05:00
|
|
|
atomic.Xaddint64(&gcController.idleMarkTime, duration)
|
2015-10-26 16:48:36 -04:00
|
|
|
}
|
|
|
|
|
|
runtime: eliminate getfull barrier from concurrent mark
Currently dedicated mark workers participate in the getfull barrier
during concurrent mark. However, the getfull barrier wasn't designed
for concurrent work and this causes no end of headaches.
In the concurrent setting, participants come and go. This makes mark
completion susceptible to live-lock: since dedicated workers are only
periodically polling for completion, it's possible for the program to
be in some transient worker each time one of the dedicated workers
wakes up to check if it can exit the getfull barrier. It also
complicates reasoning about the system because dedicated workers
participate directly in the getfull barrier, but transient workers
must instead use trygetfull because they have exit conditions that
aren't captured by getfull (e.g., fractional workers exit when
preempted). The complexity of implementing these exit conditions
contributed to #11677. Furthermore, the getfull barrier is inefficient
because we could be running user code instead of spinning on a P. In
effect, we're dedicating 25% of the CPU to marking even if that means
we have to spin to make that 25%. It also causes issues on Windows
because we can't actually sleep for 100µs (#8687).
Fix this by making dedicated workers no longer participate in the
getfull barrier. Instead, dedicated workers simply return to the
scheduler when they fail to get more work, regardless of what others
workers are doing, and the scheduler only starts new dedicated workers
if there's work available. Everything that needs to be handled by this
barrier is already handled by detection of mark completion.
This makes the system much more symmetric because all workers and
assists now use trygetfull during concurrent mark. It also loosens the
25% CPU target so that we can give some of that 25% back to user code
if there isn't enough work to keep the mark worker busy. And it
eliminates the problematic 100µs sleep on Windows during concurrent
mark (though not during mark termination).
The downside of this is that if we hit a bottleneck in the heap graph
that then expands back out, the system may shut down dedicated workers
and take a while to start them back up. We'll address this in the next
commit.
Updates #12041 and #8687.
No effect on the go1 benchmarks. This slows down the garbage benchmark
by 9%, but we'll more than make it up in the next commit.
name old time/op new time/op delta
XBenchGarbage-12 5.80ms ± 2% 6.32ms ± 4% +9.03% (p=0.000 n=20+20)
Change-Id: I65100a9ba005a8b5cf97940798918672ea9dd09b
Reviewed-on: https://go-review.googlesource.com/16297
Reviewed-by: Rick Hudson <rlh@golang.org>
2015-10-26 16:29:25 -04:00
|
|
|
// Was this the last worker and did we run out
|
|
|
|
|
// of work?
|
2015-11-02 14:09:24 -05:00
|
|
|
incnwait := atomic.Xadd(&work.nwait, +1)
|
runtime: eliminate getfull barrier from concurrent mark
Currently dedicated mark workers participate in the getfull barrier
during concurrent mark. However, the getfull barrier wasn't designed
for concurrent work and this causes no end of headaches.
In the concurrent setting, participants come and go. This makes mark
completion susceptible to live-lock: since dedicated workers are only
periodically polling for completion, it's possible for the program to
be in some transient worker each time one of the dedicated workers
wakes up to check if it can exit the getfull barrier. It also
complicates reasoning about the system because dedicated workers
participate directly in the getfull barrier, but transient workers
must instead use trygetfull because they have exit conditions that
aren't captured by getfull (e.g., fractional workers exit when
preempted). The complexity of implementing these exit conditions
contributed to #11677. Furthermore, the getfull barrier is inefficient
because we could be running user code instead of spinning on a P. In
effect, we're dedicating 25% of the CPU to marking even if that means
we have to spin to make that 25%. It also causes issues on Windows
because we can't actually sleep for 100µs (#8687).
Fix this by making dedicated workers no longer participate in the
getfull barrier. Instead, dedicated workers simply return to the
scheduler when they fail to get more work, regardless of what others
workers are doing, and the scheduler only starts new dedicated workers
if there's work available. Everything that needs to be handled by this
barrier is already handled by detection of mark completion.
This makes the system much more symmetric because all workers and
assists now use trygetfull during concurrent mark. It also loosens the
25% CPU target so that we can give some of that 25% back to user code
if there isn't enough work to keep the mark worker busy. And it
eliminates the problematic 100µs sleep on Windows during concurrent
mark (though not during mark termination).
The downside of this is that if we hit a bottleneck in the heap graph
that then expands back out, the system may shut down dedicated workers
and take a while to start them back up. We'll address this in the next
commit.
Updates #12041 and #8687.
No effect on the go1 benchmarks. This slows down the garbage benchmark
by 9%, but we'll more than make it up in the next commit.
name old time/op new time/op delta
XBenchGarbage-12 5.80ms ± 2% 6.32ms ± 4% +9.03% (p=0.000 n=20+20)
Change-Id: I65100a9ba005a8b5cf97940798918672ea9dd09b
Reviewed-on: https://go-review.googlesource.com/16297
Reviewed-by: Rick Hudson <rlh@golang.org>
2015-10-26 16:29:25 -04:00
|
|
|
if incnwait > work.nproc {
|
2016-01-19 22:45:37 -05:00
|
|
|
println("runtime: p.gcMarkWorkerMode=", _p_.gcMarkWorkerMode,
|
runtime: eliminate getfull barrier from concurrent mark
Currently dedicated mark workers participate in the getfull barrier
during concurrent mark. However, the getfull barrier wasn't designed
for concurrent work and this causes no end of headaches.
In the concurrent setting, participants come and go. This makes mark
completion susceptible to live-lock: since dedicated workers are only
periodically polling for completion, it's possible for the program to
be in some transient worker each time one of the dedicated workers
wakes up to check if it can exit the getfull barrier. It also
complicates reasoning about the system because dedicated workers
participate directly in the getfull barrier, but transient workers
must instead use trygetfull because they have exit conditions that
aren't captured by getfull (e.g., fractional workers exit when
preempted). The complexity of implementing these exit conditions
contributed to #11677. Furthermore, the getfull barrier is inefficient
because we could be running user code instead of spinning on a P. In
effect, we're dedicating 25% of the CPU to marking even if that means
we have to spin to make that 25%. It also causes issues on Windows
because we can't actually sleep for 100µs (#8687).
Fix this by making dedicated workers no longer participate in the
getfull barrier. Instead, dedicated workers simply return to the
scheduler when they fail to get more work, regardless of what others
workers are doing, and the scheduler only starts new dedicated workers
if there's work available. Everything that needs to be handled by this
barrier is already handled by detection of mark completion.
This makes the system much more symmetric because all workers and
assists now use trygetfull during concurrent mark. It also loosens the
25% CPU target so that we can give some of that 25% back to user code
if there isn't enough work to keep the mark worker busy. And it
eliminates the problematic 100µs sleep on Windows during concurrent
mark (though not during mark termination).
The downside of this is that if we hit a bottleneck in the heap graph
that then expands back out, the system may shut down dedicated workers
and take a while to start them back up. We'll address this in the next
commit.
Updates #12041 and #8687.
No effect on the go1 benchmarks. This slows down the garbage benchmark
by 9%, but we'll more than make it up in the next commit.
name old time/op new time/op delta
XBenchGarbage-12 5.80ms ± 2% 6.32ms ± 4% +9.03% (p=0.000 n=20+20)
Change-Id: I65100a9ba005a8b5cf97940798918672ea9dd09b
Reviewed-on: https://go-review.googlesource.com/16297
Reviewed-by: Rick Hudson <rlh@golang.org>
2015-10-26 16:29:25 -04:00
|
|
|
"work.nwait=", incnwait, "work.nproc=", work.nproc)
|
|
|
|
|
throw("work.nwait > work.nproc")
|
2015-06-01 18:16:03 -04:00
|
|
|
}
|
runtime: multi-threaded, utilization-scheduled background mark
Currently, the concurrent mark phase is performed by the main GC
goroutine. Prior to the previous commit enabling preemption, this
caused marking to always consume 1/GOMAXPROCS of the available CPU
time. If GOMAXPROCS=1, this meant background GC would consume 100% of
the CPU (effectively a STW). If GOMAXPROCS>4, background GC would use
less than the goal of 25%. If GOMAXPROCS=4, background GC would use
the goal 25%, but if the mutator wasn't using the remaining 75%,
background marking wouldn't take advantage of the idle time. Enabling
preemption in the previous commit made GC miss CPU targets in
completely different ways, but set us up to bring everything back in
line.
This change replaces the fixed GC goroutine with per-P background mark
goroutines. Once started, these goroutines don't go in the standard
run queues; instead, they are scheduled specially such that the time
spent in mutator assists and the background mark goroutines totals 25%
of the CPU time available to the program. Furthermore, this lets
background marking take advantage of idle Ps, which significantly
boosts GC performance for applications that under-utilize the CPU.
This requires also changing how time is reported for gctrace, so this
change splits the concurrent mark CPU time into assist/background/idle
scanning.
This also requires increasing the size of the StackRecord slice used
in a GoroutineProfile test.
Change-Id: I0936ff907d2cee6cb687a208f2df47e8988e3157
Reviewed-on: https://go-review.googlesource.com/8850
Reviewed-by: Rick Hudson <rlh@golang.org>
2015-03-23 21:07:33 -04:00
|
|
|
|
2015-04-22 17:44:36 -04:00
|
|
|
// If this worker reached a background mark completion
|
|
|
|
|
// point, signal the main GC goroutine.
|
runtime: eliminate getfull barrier from concurrent mark
Currently dedicated mark workers participate in the getfull barrier
during concurrent mark. However, the getfull barrier wasn't designed
for concurrent work and this causes no end of headaches.
In the concurrent setting, participants come and go. This makes mark
completion susceptible to live-lock: since dedicated workers are only
periodically polling for completion, it's possible for the program to
be in some transient worker each time one of the dedicated workers
wakes up to check if it can exit the getfull barrier. It also
complicates reasoning about the system because dedicated workers
participate directly in the getfull barrier, but transient workers
must instead use trygetfull because they have exit conditions that
aren't captured by getfull (e.g., fractional workers exit when
preempted). The complexity of implementing these exit conditions
contributed to #11677. Furthermore, the getfull barrier is inefficient
because we could be running user code instead of spinning on a P. In
effect, we're dedicating 25% of the CPU to marking even if that means
we have to spin to make that 25%. It also causes issues on Windows
because we can't actually sleep for 100µs (#8687).
Fix this by making dedicated workers no longer participate in the
getfull barrier. Instead, dedicated workers simply return to the
scheduler when they fail to get more work, regardless of what others
workers are doing, and the scheduler only starts new dedicated workers
if there's work available. Everything that needs to be handled by this
barrier is already handled by detection of mark completion.
This makes the system much more symmetric because all workers and
assists now use trygetfull during concurrent mark. It also loosens the
25% CPU target so that we can give some of that 25% back to user code
if there isn't enough work to keep the mark worker busy. And it
eliminates the problematic 100µs sleep on Windows during concurrent
mark (though not during mark termination).
The downside of this is that if we hit a bottleneck in the heap graph
that then expands back out, the system may shut down dedicated workers
and take a while to start them back up. We'll address this in the next
commit.
Updates #12041 and #8687.
No effect on the go1 benchmarks. This slows down the garbage benchmark
by 9%, but we'll more than make it up in the next commit.
name old time/op new time/op delta
XBenchGarbage-12 5.80ms ± 2% 6.32ms ± 4% +9.03% (p=0.000 n=20+20)
Change-Id: I65100a9ba005a8b5cf97940798918672ea9dd09b
Reviewed-on: https://go-review.googlesource.com/16297
Reviewed-by: Rick Hudson <rlh@golang.org>
2015-10-26 16:29:25 -04:00
|
|
|
if incnwait == work.nproc && !gcMarkWorkAvailable(nil) {
|
2015-10-26 11:27:37 -04:00
|
|
|
// Make this G preemptible and disassociate it
|
|
|
|
|
// as the worker for this P so
|
|
|
|
|
// findRunnableGCWorker doesn't try to
|
|
|
|
|
// schedule it.
|
2016-01-26 14:44:58 -05:00
|
|
|
_p_.gcBgMarkWorker.set(nil)
|
runtime: never pass stack pointers to gopark
gopark calls the unlock function after setting the G to _Gwaiting.
This means it's generally unsafe to access the G's stack from the
unlock function because the G may start running on another P. Once we
start shrinking stacks concurrently, a stack shrink could also move
the stack the moment after it enters _Gwaiting and before the unlock
function is called.
Document this restriction and fix the two places where we currently
violate it.
This is unlikely to be a problem in practice for these two places
right now, but they're already skating on thin ice. For example, the
following sequence could in principle cause corruption, deadlock, or a
panic in the select code:
On M1/P1:
1. G1 selects on channels A and B.
2. selectgoImpl calls gopark.
3. gopark puts G1 in _Gwaiting.
4. gopark calls selparkcommit.
5. selparkcommit releases the lock on channel A.
On M2/P2:
6. G2 sends to channel A.
7. The send puts G1 in _Grunnable and puts it on P2's run queue.
8. The scheduler runs, selects G1, puts it in _Grunning, and resumes G1.
9. On G1, the sellock immediately following the gopark gets called.
10. sellock grows and moves the stack.
On M1/P1:
11. selparkcommit continues to scan the lock order for the next
channel to unlock, but it's now reading from a freed (and possibly
reused) stack.
This shouldn't happen in practice because step 10 isn't the first call
to sellock, so the stack should already be big enough. However, once
we start shrinking stacks concurrently, this reasoning won't work any
more.
For #12967.
Change-Id: I3660c5be37e5be9f87433cb8141bdfdf37fadc4c
Reviewed-on: https://go-review.googlesource.com/20038
Reviewed-by: Rick Hudson <rlh@golang.org>
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2016-02-26 10:50:54 -05:00
|
|
|
releasem(park.m.ptr())
|
2015-10-26 11:27:37 -04:00
|
|
|
|
2015-10-24 21:30:59 -04:00
|
|
|
gcMarkDone()
|
2015-10-26 11:27:37 -04:00
|
|
|
|
2016-01-19 22:45:37 -05:00
|
|
|
// Disable preemption and prepare to reattach
|
|
|
|
|
// to the P.
|
2015-10-26 11:27:37 -04:00
|
|
|
//
|
|
|
|
|
// We may be running on a different P at this
|
2016-01-19 22:45:37 -05:00
|
|
|
// point, so we can't reattach until this G is
|
|
|
|
|
// parked.
|
runtime: never pass stack pointers to gopark
gopark calls the unlock function after setting the G to _Gwaiting.
This means it's generally unsafe to access the G's stack from the
unlock function because the G may start running on another P. Once we
start shrinking stacks concurrently, a stack shrink could also move
the stack the moment after it enters _Gwaiting and before the unlock
function is called.
Document this restriction and fix the two places where we currently
violate it.
This is unlikely to be a problem in practice for these two places
right now, but they're already skating on thin ice. For example, the
following sequence could in principle cause corruption, deadlock, or a
panic in the select code:
On M1/P1:
1. G1 selects on channels A and B.
2. selectgoImpl calls gopark.
3. gopark puts G1 in _Gwaiting.
4. gopark calls selparkcommit.
5. selparkcommit releases the lock on channel A.
On M2/P2:
6. G2 sends to channel A.
7. The send puts G1 in _Grunnable and puts it on P2's run queue.
8. The scheduler runs, selects G1, puts it in _Grunning, and resumes G1.
9. On G1, the sellock immediately following the gopark gets called.
10. sellock grows and moves the stack.
On M1/P1:
11. selparkcommit continues to scan the lock order for the next
channel to unlock, but it's now reading from a freed (and possibly
reused) stack.
This shouldn't happen in practice because step 10 isn't the first call
to sellock, so the stack should already be big enough. However, once
we start shrinking stacks concurrently, this reasoning won't work any
more.
For #12967.
Change-Id: I3660c5be37e5be9f87433cb8141bdfdf37fadc4c
Reviewed-on: https://go-review.googlesource.com/20038
Reviewed-by: Rick Hudson <rlh@golang.org>
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2016-02-26 10:50:54 -05:00
|
|
|
park.m.set(acquirem())
|
|
|
|
|
park.attach.set(_p_)
|
runtime: multi-threaded, utilization-scheduled background mark
Currently, the concurrent mark phase is performed by the main GC
goroutine. Prior to the previous commit enabling preemption, this
caused marking to always consume 1/GOMAXPROCS of the available CPU
time. If GOMAXPROCS=1, this meant background GC would consume 100% of
the CPU (effectively a STW). If GOMAXPROCS>4, background GC would use
less than the goal of 25%. If GOMAXPROCS=4, background GC would use
the goal 25%, but if the mutator wasn't using the remaining 75%,
background marking wouldn't take advantage of the idle time. Enabling
preemption in the previous commit made GC miss CPU targets in
completely different ways, but set us up to bring everything back in
line.
This change replaces the fixed GC goroutine with per-P background mark
goroutines. Once started, these goroutines don't go in the standard
run queues; instead, they are scheduled specially such that the time
spent in mutator assists and the background mark goroutines totals 25%
of the CPU time available to the program. Furthermore, this lets
background marking take advantage of idle Ps, which significantly
boosts GC performance for applications that under-utilize the CPU.
This requires also changing how time is reported for gctrace, so this
change splits the concurrent mark CPU time into assist/background/idle
scanning.
This also requires increasing the size of the StackRecord slice used
in a GoroutineProfile test.
Change-Id: I0936ff907d2cee6cb687a208f2df47e8988e3157
Reviewed-on: https://go-review.googlesource.com/8850
Reviewed-by: Rick Hudson <rlh@golang.org>
2015-03-23 21:07:33 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-01 18:16:03 -04:00
|
|
|
// gcMarkWorkAvailable returns true if executing a mark worker
|
2015-10-19 13:35:25 -04:00
|
|
|
// on p is potentially useful. p may be nil, in which case it only
|
|
|
|
|
// checks the global sources of work.
|
2015-05-18 16:02:37 -04:00
|
|
|
func gcMarkWorkAvailable(p *p) bool {
|
2015-10-19 13:35:25 -04:00
|
|
|
if p != nil && !p.gcw.empty() {
|
2015-05-18 16:02:37 -04:00
|
|
|
return true
|
|
|
|
|
}
|
2017-03-07 16:38:29 -05:00
|
|
|
if !work.full.empty() {
|
2015-05-18 16:02:37 -04:00
|
|
|
return true // global work available
|
|
|
|
|
}
|
runtime: perform concurrent scan in GC workers
Currently the concurrent root scan is performed in its entirety by the
GC coordinator before entering concurrent mark (which enables GC
workers). This scan is done sequentially, which can prolong the scan
phase, delay the mark phase, and means that the scan phase does not
obey the 25% CPU goal. Furthermore, there's no need to complete the
root scan before starting marking (in fact, we already allow GC
assists to happen during the scan phase), so this acts as an
unnecessary barrier between root scanning and marking.
This change shifts the root scan work out of the GC coordinator and in
to the GC workers. The coordinator simply sets up the scan state and
enqueues the right number of root scan jobs. The GC workers then drain
the root scan jobs prior to draining heap scan jobs.
This parallelizes the root scan process, makes it obey the 25% CPU
goal, and effectively eliminates root scanning as an isolated phase,
allowing the system to smoothly transition from root scanning to heap
marking. This also eliminates a major non-STW responsibility of the GC
coordinator, which will make it easier to switch to a decentralized
state machine. Finally, it puts us in a good position to perform root
scanning in assists as well, which will help satisfy assists at the
beginning of the GC cycle.
This is mostly straightforward. One tricky aspect is that we have to
deal with preemption deadlock: where two non-preemptible gorountines
are trying to preempt each other to perform a stack scan. Given the
context where this happens, the only instance of this is two
background workers trying to scan each other. We avoid this by simply
not scanning the stacks of background workers during the concurrent
phase; this is safe because we'll scan them during mark termination
(and their stacks are *very* small and should not contain any new
pointers).
This change also switches the root marking during mark termination to
use the same gcDrain-based code path as concurrent mark. This
shouldn't affect performance because STW root marking was already
parallel and tasks switched to heap marking immediately when no more
root marking tasks were available. However, it simplifies the code and
unifies these code paths.
This has negligible effect on the go1 benchmarks. It slightly slows
down the garbage benchmark, possibly by making GC run slightly more
frequently.
name old time/op new time/op delta
XBenchGarbage-12 5.10ms ± 1% 5.24ms ± 1% +2.87% (p=0.000 n=18+18)
name old time/op new time/op delta
BinaryTree17-12 3.25s ± 3% 3.20s ± 5% -1.57% (p=0.013 n=20+20)
Fannkuch11-12 2.45s ± 1% 2.46s ± 1% +0.38% (p=0.019 n=20+18)
FmtFprintfEmpty-12 49.7ns ± 3% 49.9ns ± 4% ~ (p=0.851 n=19+20)
FmtFprintfString-12 170ns ± 2% 170ns ± 1% ~ (p=0.775 n=20+19)
FmtFprintfInt-12 161ns ± 1% 160ns ± 1% -0.78% (p=0.000 n=19+18)
FmtFprintfIntInt-12 267ns ± 1% 270ns ± 1% +1.04% (p=0.000 n=19+19)
FmtFprintfPrefixedInt-12 238ns ± 2% 238ns ± 1% ~ (p=0.133 n=18+19)
FmtFprintfFloat-12 311ns ± 1% 310ns ± 2% -0.35% (p=0.023 n=20+19)
FmtManyArgs-12 1.08µs ± 1% 1.06µs ± 1% -2.31% (p=0.000 n=20+20)
GobDecode-12 8.65ms ± 1% 8.63ms ± 1% ~ (p=0.377 n=18+20)
GobEncode-12 6.49ms ± 1% 6.52ms ± 1% +0.37% (p=0.015 n=20+20)
Gzip-12 319ms ± 3% 318ms ± 1% ~ (p=0.975 n=19+17)
Gunzip-12 41.9ms ± 1% 42.1ms ± 2% +0.65% (p=0.004 n=19+20)
HTTPClientServer-12 61.7µs ± 1% 62.6µs ± 1% +1.40% (p=0.000 n=18+20)
JSONEncode-12 16.8ms ± 1% 16.9ms ± 1% ~ (p=0.239 n=20+18)
JSONDecode-12 58.4ms ± 1% 60.7ms ± 1% +3.85% (p=0.000 n=19+20)
Mandelbrot200-12 3.86ms ± 0% 3.86ms ± 1% ~ (p=0.092 n=18+19)
GoParse-12 3.75ms ± 2% 3.75ms ± 2% ~ (p=0.708 n=19+20)
RegexpMatchEasy0_32-12 100ns ± 1% 100ns ± 2% +0.60% (p=0.010 n=17+20)
RegexpMatchEasy0_1K-12 341ns ± 1% 342ns ± 2% ~ (p=0.203 n=20+19)
RegexpMatchEasy1_32-12 82.5ns ± 2% 83.2ns ± 2% +0.83% (p=0.007 n=19+19)
RegexpMatchEasy1_1K-12 495ns ± 1% 495ns ± 2% ~ (p=0.970 n=19+18)
RegexpMatchMedium_32-12 130ns ± 2% 130ns ± 2% +0.59% (p=0.039 n=19+20)
RegexpMatchMedium_1K-12 39.2µs ± 1% 39.3µs ± 1% ~ (p=0.214 n=18+18)
RegexpMatchHard_32-12 2.03µs ± 2% 2.02µs ± 1% ~ (p=0.166 n=18+19)
RegexpMatchHard_1K-12 61.0µs ± 1% 60.9µs ± 1% ~ (p=0.169 n=20+18)
Revcomp-12 533ms ± 1% 535ms ± 1% ~ (p=0.071 n=19+17)
Template-12 68.1ms ± 2% 73.0ms ± 1% +7.26% (p=0.000 n=19+20)
TimeParse-12 355ns ± 2% 356ns ± 2% ~ (p=0.530 n=19+20)
TimeFormat-12 357ns ± 2% 347ns ± 1% -2.59% (p=0.000 n=20+19)
[Geo mean] 62.1µs 62.3µs +0.31%
name old speed new speed delta
GobDecode-12 88.7MB/s ± 1% 88.9MB/s ± 1% ~ (p=0.377 n=18+20)
GobEncode-12 118MB/s ± 1% 118MB/s ± 1% -0.37% (p=0.015 n=20+20)
Gzip-12 60.9MB/s ± 3% 60.9MB/s ± 1% ~ (p=0.944 n=19+17)
Gunzip-12 464MB/s ± 1% 461MB/s ± 2% -0.64% (p=0.004 n=19+20)
JSONEncode-12 115MB/s ± 1% 115MB/s ± 1% ~ (p=0.236 n=20+18)
JSONDecode-12 33.2MB/s ± 1% 32.0MB/s ± 1% -3.71% (p=0.000 n=19+20)
GoParse-12 15.5MB/s ± 2% 15.5MB/s ± 2% ~ (p=0.702 n=19+20)
RegexpMatchEasy0_32-12 320MB/s ± 1% 318MB/s ± 2% ~ (p=0.094 n=18+20)
RegexpMatchEasy0_1K-12 3.00GB/s ± 1% 2.99GB/s ± 1% ~ (p=0.194 n=20+19)
RegexpMatchEasy1_32-12 388MB/s ± 2% 385MB/s ± 2% -0.83% (p=0.008 n=19+19)
RegexpMatchEasy1_1K-12 2.07GB/s ± 1% 2.07GB/s ± 1% ~ (p=0.964 n=19+18)
RegexpMatchMedium_32-12 7.68MB/s ± 1% 7.64MB/s ± 2% -0.57% (p=0.020 n=19+20)
RegexpMatchMedium_1K-12 26.1MB/s ± 1% 26.1MB/s ± 1% ~ (p=0.211 n=18+18)
RegexpMatchHard_32-12 15.8MB/s ± 1% 15.8MB/s ± 1% ~ (p=0.180 n=18+19)
RegexpMatchHard_1K-12 16.8MB/s ± 1% 16.8MB/s ± 2% ~ (p=0.236 n=20+19)
Revcomp-12 477MB/s ± 1% 475MB/s ± 1% ~ (p=0.071 n=19+17)
Template-12 28.5MB/s ± 2% 26.6MB/s ± 1% -6.77% (p=0.000 n=19+20)
[Geo mean] 100MB/s 99.0MB/s -0.82%
Change-Id: I875bf6ceb306d1ee2f470cabf88aa6ede27c47a0
Reviewed-on: https://go-review.googlesource.com/16059
Reviewed-by: Rick Hudson <rlh@golang.org>
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2015-10-19 13:46:32 -04:00
|
|
|
if work.markrootNext < work.markrootJobs {
|
|
|
|
|
return true // root scan work available
|
|
|
|
|
}
|
2015-05-18 16:02:37 -04:00
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-19 16:43:27 -05:00
|
|
|
// gcMark runs the mark (or, for concurrent GC, mark termination)
|
2016-09-11 16:55:34 -04:00
|
|
|
// All gcWork caches must be empty.
|
2015-02-19 15:48:40 -05:00
|
|
|
// STW is in effect at this point.
|
|
|
|
|
//TODO go:nowritebarrier
|
2017-04-03 12:10:56 -04:00
|
|
|
func gcMark(start_time int64) {
|
2014-11-11 17:05:02 -05:00
|
|
|
if debug.allocfreetrace > 0 {
|
|
|
|
|
tracegc()
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-05 17:33:08 -05:00
|
|
|
if gcphase != _GCmarktermination {
|
|
|
|
|
throw("in gcMark expecting to see gcphase as _GCmarktermination")
|
|
|
|
|
}
|
[dev.cc] runtime: delete scalararg, ptrarg; rename onM to systemstack
Scalararg and ptrarg are not "signal safe".
Go code filling them out can be interrupted by a signal,
and then the signal handler runs, and if it also ends up
in Go code that uses scalararg or ptrarg, now the old
values have been smashed.
For the pieces of code that do need to run in a signal handler,
we introduced onM_signalok, which is really just onM
except that the _signalok is meant to convey that the caller
asserts that scalarg and ptrarg will be restored to their old
values after the call (instead of the usual behavior, zeroing them).
Scalararg and ptrarg are also untyped and therefore error-prone.
Go code can always pass a closure instead of using scalararg
and ptrarg; they were only really necessary for C code.
And there's no more C code.
For all these reasons, delete scalararg and ptrarg, converting
the few remaining references to use closures.
Once those are gone, there is no need for a distinction between
onM and onM_signalok, so replace both with a single function
equivalent to the current onM_signalok (that is, it can be called
on any of the curg, g0, and gsignal stacks).
The name onM and the phrase 'm stack' are misnomers,
because on most system an M has two system stacks:
the main thread stack and the signal handling stack.
Correct the misnomer by naming the replacement function systemstack.
Fix a few references to "M stack" in code.
The main motivation for this change is to eliminate scalararg/ptrarg.
Rick and I have already seen them cause problems because
the calling sequence m.ptrarg[0] = p is a heap pointer assignment,
so it gets a write barrier. The write barrier also uses onM, so it has
all the same problems as if it were being invoked by a signal handler.
We worked around this by saving and restoring the old values
and by calling onM_signalok, but there's no point in keeping this nice
home for bugs around any longer.
This CL also changes funcline to return the file name as a result
instead of filling in a passed-in *string. (The *string signature is
left over from when the code was written in and called from C.)
That's arguably an unrelated change, except that once I had done
the ptrarg/scalararg/onM cleanup I started getting false positives
about the *string argument escaping (not allowed in package runtime).
The compiler is wrong, but the easiest fix is to write the code like
Go code instead of like C code. I am a bit worried that the compiler
is wrong because of some use of uninitialized memory in the escape
analysis. If that's the reason, it will go away when we convert the
compiler to Go. (And if not, we'll debug it the next time.)
LGTM=khr
R=r, khr
CC=austin, golang-codereviews, iant, rlh
https://golang.org/cl/174950043
2014-11-12 14:54:31 -05:00
|
|
|
work.tstart = start_time
|
2014-11-11 17:05:02 -05:00
|
|
|
|
2015-10-16 16:52:26 -04:00
|
|
|
// Queue root marking jobs.
|
runtime: perform concurrent scan in GC workers
Currently the concurrent root scan is performed in its entirety by the
GC coordinator before entering concurrent mark (which enables GC
workers). This scan is done sequentially, which can prolong the scan
phase, delay the mark phase, and means that the scan phase does not
obey the 25% CPU goal. Furthermore, there's no need to complete the
root scan before starting marking (in fact, we already allow GC
assists to happen during the scan phase), so this acts as an
unnecessary barrier between root scanning and marking.
This change shifts the root scan work out of the GC coordinator and in
to the GC workers. The coordinator simply sets up the scan state and
enqueues the right number of root scan jobs. The GC workers then drain
the root scan jobs prior to draining heap scan jobs.
This parallelizes the root scan process, makes it obey the 25% CPU
goal, and effectively eliminates root scanning as an isolated phase,
allowing the system to smoothly transition from root scanning to heap
marking. This also eliminates a major non-STW responsibility of the GC
coordinator, which will make it easier to switch to a decentralized
state machine. Finally, it puts us in a good position to perform root
scanning in assists as well, which will help satisfy assists at the
beginning of the GC cycle.
This is mostly straightforward. One tricky aspect is that we have to
deal with preemption deadlock: where two non-preemptible gorountines
are trying to preempt each other to perform a stack scan. Given the
context where this happens, the only instance of this is two
background workers trying to scan each other. We avoid this by simply
not scanning the stacks of background workers during the concurrent
phase; this is safe because we'll scan them during mark termination
(and their stacks are *very* small and should not contain any new
pointers).
This change also switches the root marking during mark termination to
use the same gcDrain-based code path as concurrent mark. This
shouldn't affect performance because STW root marking was already
parallel and tasks switched to heap marking immediately when no more
root marking tasks were available. However, it simplifies the code and
unifies these code paths.
This has negligible effect on the go1 benchmarks. It slightly slows
down the garbage benchmark, possibly by making GC run slightly more
frequently.
name old time/op new time/op delta
XBenchGarbage-12 5.10ms ± 1% 5.24ms ± 1% +2.87% (p=0.000 n=18+18)
name old time/op new time/op delta
BinaryTree17-12 3.25s ± 3% 3.20s ± 5% -1.57% (p=0.013 n=20+20)
Fannkuch11-12 2.45s ± 1% 2.46s ± 1% +0.38% (p=0.019 n=20+18)
FmtFprintfEmpty-12 49.7ns ± 3% 49.9ns ± 4% ~ (p=0.851 n=19+20)
FmtFprintfString-12 170ns ± 2% 170ns ± 1% ~ (p=0.775 n=20+19)
FmtFprintfInt-12 161ns ± 1% 160ns ± 1% -0.78% (p=0.000 n=19+18)
FmtFprintfIntInt-12 267ns ± 1% 270ns ± 1% +1.04% (p=0.000 n=19+19)
FmtFprintfPrefixedInt-12 238ns ± 2% 238ns ± 1% ~ (p=0.133 n=18+19)
FmtFprintfFloat-12 311ns ± 1% 310ns ± 2% -0.35% (p=0.023 n=20+19)
FmtManyArgs-12 1.08µs ± 1% 1.06µs ± 1% -2.31% (p=0.000 n=20+20)
GobDecode-12 8.65ms ± 1% 8.63ms ± 1% ~ (p=0.377 n=18+20)
GobEncode-12 6.49ms ± 1% 6.52ms ± 1% +0.37% (p=0.015 n=20+20)
Gzip-12 319ms ± 3% 318ms ± 1% ~ (p=0.975 n=19+17)
Gunzip-12 41.9ms ± 1% 42.1ms ± 2% +0.65% (p=0.004 n=19+20)
HTTPClientServer-12 61.7µs ± 1% 62.6µs ± 1% +1.40% (p=0.000 n=18+20)
JSONEncode-12 16.8ms ± 1% 16.9ms ± 1% ~ (p=0.239 n=20+18)
JSONDecode-12 58.4ms ± 1% 60.7ms ± 1% +3.85% (p=0.000 n=19+20)
Mandelbrot200-12 3.86ms ± 0% 3.86ms ± 1% ~ (p=0.092 n=18+19)
GoParse-12 3.75ms ± 2% 3.75ms ± 2% ~ (p=0.708 n=19+20)
RegexpMatchEasy0_32-12 100ns ± 1% 100ns ± 2% +0.60% (p=0.010 n=17+20)
RegexpMatchEasy0_1K-12 341ns ± 1% 342ns ± 2% ~ (p=0.203 n=20+19)
RegexpMatchEasy1_32-12 82.5ns ± 2% 83.2ns ± 2% +0.83% (p=0.007 n=19+19)
RegexpMatchEasy1_1K-12 495ns ± 1% 495ns ± 2% ~ (p=0.970 n=19+18)
RegexpMatchMedium_32-12 130ns ± 2% 130ns ± 2% +0.59% (p=0.039 n=19+20)
RegexpMatchMedium_1K-12 39.2µs ± 1% 39.3µs ± 1% ~ (p=0.214 n=18+18)
RegexpMatchHard_32-12 2.03µs ± 2% 2.02µs ± 1% ~ (p=0.166 n=18+19)
RegexpMatchHard_1K-12 61.0µs ± 1% 60.9µs ± 1% ~ (p=0.169 n=20+18)
Revcomp-12 533ms ± 1% 535ms ± 1% ~ (p=0.071 n=19+17)
Template-12 68.1ms ± 2% 73.0ms ± 1% +7.26% (p=0.000 n=19+20)
TimeParse-12 355ns ± 2% 356ns ± 2% ~ (p=0.530 n=19+20)
TimeFormat-12 357ns ± 2% 347ns ± 1% -2.59% (p=0.000 n=20+19)
[Geo mean] 62.1µs 62.3µs +0.31%
name old speed new speed delta
GobDecode-12 88.7MB/s ± 1% 88.9MB/s ± 1% ~ (p=0.377 n=18+20)
GobEncode-12 118MB/s ± 1% 118MB/s ± 1% -0.37% (p=0.015 n=20+20)
Gzip-12 60.9MB/s ± 3% 60.9MB/s ± 1% ~ (p=0.944 n=19+17)
Gunzip-12 464MB/s ± 1% 461MB/s ± 2% -0.64% (p=0.004 n=19+20)
JSONEncode-12 115MB/s ± 1% 115MB/s ± 1% ~ (p=0.236 n=20+18)
JSONDecode-12 33.2MB/s ± 1% 32.0MB/s ± 1% -3.71% (p=0.000 n=19+20)
GoParse-12 15.5MB/s ± 2% 15.5MB/s ± 2% ~ (p=0.702 n=19+20)
RegexpMatchEasy0_32-12 320MB/s ± 1% 318MB/s ± 2% ~ (p=0.094 n=18+20)
RegexpMatchEasy0_1K-12 3.00GB/s ± 1% 2.99GB/s ± 1% ~ (p=0.194 n=20+19)
RegexpMatchEasy1_32-12 388MB/s ± 2% 385MB/s ± 2% -0.83% (p=0.008 n=19+19)
RegexpMatchEasy1_1K-12 2.07GB/s ± 1% 2.07GB/s ± 1% ~ (p=0.964 n=19+18)
RegexpMatchMedium_32-12 7.68MB/s ± 1% 7.64MB/s ± 2% -0.57% (p=0.020 n=19+20)
RegexpMatchMedium_1K-12 26.1MB/s ± 1% 26.1MB/s ± 1% ~ (p=0.211 n=18+18)
RegexpMatchHard_32-12 15.8MB/s ± 1% 15.8MB/s ± 1% ~ (p=0.180 n=18+19)
RegexpMatchHard_1K-12 16.8MB/s ± 1% 16.8MB/s ± 2% ~ (p=0.236 n=20+19)
Revcomp-12 477MB/s ± 1% 475MB/s ± 1% ~ (p=0.071 n=19+17)
Template-12 28.5MB/s ± 2% 26.6MB/s ± 1% -6.77% (p=0.000 n=19+20)
[Geo mean] 100MB/s 99.0MB/s -0.82%
Change-Id: I875bf6ceb306d1ee2f470cabf88aa6ede27c47a0
Reviewed-on: https://go-review.googlesource.com/16059
Reviewed-by: Rick Hudson <rlh@golang.org>
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2015-10-19 13:46:32 -04:00
|
|
|
gcMarkRootPrepare()
|
2015-10-16 16:52:26 -04:00
|
|
|
|
2014-11-11 17:05:02 -05:00
|
|
|
work.nwait = 0
|
|
|
|
|
work.ndone = 0
|
|
|
|
|
work.nproc = uint32(gcprocs())
|
2014-11-15 08:00:38 -05:00
|
|
|
|
2017-02-09 11:50:26 -05:00
|
|
|
if work.full == 0 && work.nDataRoots+work.nBSSRoots+work.nSpanRoots+work.nStackRoots == 0 {
|
runtime: avoid getfull() barrier most of the time
With the hybrid barrier, unless we're doing a STW GC or hit a very
rare race (~once per all.bash) that can start mark termination before
all of the work is drained, we don't need to drain the work queue at
all. Even draining an empty work queue is rather expensive since we
have to enter the getfull() barrier, so it's worth avoiding this.
Conveniently, it's quite easy to detect whether or not we actually
need the getufull() barrier: since the world is stopped when we enter
mark termination, everything must have flushed its work to the work
queue, so we can just check the queue. If the queue is empty and we
haven't queued up any jobs that may create more work (which should
always be the case with the hybrid barrier), we can simply have all GC
workers perform non-blocking drains.
Also conveniently, this solution is quite safe. If we do somehow screw
something up and there's work on the work queue, some worker will
still process it, it just may not happen in parallel.
This is not the "right" solution, but it's simple, expedient,
low-risk, and maintains compatibility with debug.gcrescanstacks. When
we remove the gcrescanstacks fallback in Go 1.9, we should also fix
the race that starts mark termination early, and then we can eliminate
work draining from mark termination.
Updates #17503.
Change-Id: I7b3cd5de6a248ab29d78c2b42aed8b7443641361
Reviewed-on: https://go-review.googlesource.com/32186
Reviewed-by: Rick Hudson <rlh@golang.org>
2016-10-26 17:05:41 -04:00
|
|
|
// There's no work on the work queue and no root jobs
|
|
|
|
|
// that can produce work, so don't bother entering the
|
|
|
|
|
// getfull() barrier.
|
|
|
|
|
//
|
2017-02-09 11:50:26 -05:00
|
|
|
// This will be the situation the vast majority of the
|
|
|
|
|
// time after concurrent mark. However, we still need
|
|
|
|
|
// a fallback for STW GC and because there are some
|
|
|
|
|
// known races that occasionally leave work around for
|
|
|
|
|
// mark termination.
|
runtime: avoid getfull() barrier most of the time
With the hybrid barrier, unless we're doing a STW GC or hit a very
rare race (~once per all.bash) that can start mark termination before
all of the work is drained, we don't need to drain the work queue at
all. Even draining an empty work queue is rather expensive since we
have to enter the getfull() barrier, so it's worth avoiding this.
Conveniently, it's quite easy to detect whether or not we actually
need the getufull() barrier: since the world is stopped when we enter
mark termination, everything must have flushed its work to the work
queue, so we can just check the queue. If the queue is empty and we
haven't queued up any jobs that may create more work (which should
always be the case with the hybrid barrier), we can simply have all GC
workers perform non-blocking drains.
Also conveniently, this solution is quite safe. If we do somehow screw
something up and there's work on the work queue, some worker will
still process it, it just may not happen in parallel.
This is not the "right" solution, but it's simple, expedient,
low-risk, and maintains compatibility with debug.gcrescanstacks. When
we remove the gcrescanstacks fallback in Go 1.9, we should also fix
the race that starts mark termination early, and then we can eliminate
work draining from mark termination.
Updates #17503.
Change-Id: I7b3cd5de6a248ab29d78c2b42aed8b7443641361
Reviewed-on: https://go-review.googlesource.com/32186
Reviewed-by: Rick Hudson <rlh@golang.org>
2016-10-26 17:05:41 -04:00
|
|
|
//
|
|
|
|
|
// We're still hedging our bets here: if we do
|
|
|
|
|
// accidentally produce some work, we'll still process
|
|
|
|
|
// it, just not necessarily in parallel.
|
|
|
|
|
//
|
2017-02-09 11:50:26 -05:00
|
|
|
// TODO(austin): Fix the races and and remove
|
runtime: avoid getfull() barrier most of the time
With the hybrid barrier, unless we're doing a STW GC or hit a very
rare race (~once per all.bash) that can start mark termination before
all of the work is drained, we don't need to drain the work queue at
all. Even draining an empty work queue is rather expensive since we
have to enter the getfull() barrier, so it's worth avoiding this.
Conveniently, it's quite easy to detect whether or not we actually
need the getufull() barrier: since the world is stopped when we enter
mark termination, everything must have flushed its work to the work
queue, so we can just check the queue. If the queue is empty and we
haven't queued up any jobs that may create more work (which should
always be the case with the hybrid barrier), we can simply have all GC
workers perform non-blocking drains.
Also conveniently, this solution is quite safe. If we do somehow screw
something up and there's work on the work queue, some worker will
still process it, it just may not happen in parallel.
This is not the "right" solution, but it's simple, expedient,
low-risk, and maintains compatibility with debug.gcrescanstacks. When
we remove the gcrescanstacks fallback in Go 1.9, we should also fix
the race that starts mark termination early, and then we can eliminate
work draining from mark termination.
Updates #17503.
Change-Id: I7b3cd5de6a248ab29d78c2b42aed8b7443641361
Reviewed-on: https://go-review.googlesource.com/32186
Reviewed-by: Rick Hudson <rlh@golang.org>
2016-10-26 17:05:41 -04:00
|
|
|
// work draining from mark termination so we don't
|
|
|
|
|
// need the fallback path.
|
|
|
|
|
work.helperDrainBlock = false
|
|
|
|
|
} else {
|
|
|
|
|
work.helperDrainBlock = true
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-11 17:05:02 -05:00
|
|
|
if work.nproc > 1 {
|
|
|
|
|
noteclear(&work.alldone)
|
|
|
|
|
helpgc(int32(work.nproc))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
gchelperstart()
|
runtime: replace per-M workbuf cache with per-P gcWork cache
Currently, each M has a cache of the most recently used *workbuf. This
is used primarily by the write barrier so it doesn't have to access
the global workbuf lists on every write barrier. It's also used by
stack scanning because it's convenient.
This cache is important for write barrier performance, but this
particular approach has several downsides. It's faster than no cache,
but far from optimal (as the benchmarks below show). It's complex:
access to the cache is sprinkled through most of the workbuf list
operations and it requires special care to transform into and back out
of the gcWork cache that's actually used for scanning and marking. It
requires atomic exchanges to take ownership of the cached workbuf and
to return it to the M's cache even though it's almost always used by
only the current M. Since it's per-M, flushing these caches is O(# of
Ms), which may be high. And it has some significant subtleties: for
example, in general the cache shouldn't be used after the
harvestwbufs() in mark termination because it could hide work from
mark termination, but stack scanning can happen after this and *will*
use the cache (but it turns out this is okay because it will always be
followed by a getfull(), which drains the cache).
This change replaces this cache with a per-P gcWork object. This
gcWork cache can be used directly by scanning and marking (as long as
preemption is disabled, which is a general requirement of gcWork).
Since it's per-P, it doesn't require synchronization, which simplifies
things and means the only atomic operations in the write barrier are
occasionally fetching new work buffers and setting a mark bit if the
object isn't already marked. This cache can be flushed in O(# of Ps),
which is generally small. It follows a simple flushing rule: the cache
can be used during any phase, but during mark termination it must be
flushed before allowing preemption. This also makes the dispose during
mutator assist no longer necessary, which eliminates the vast majority
of gcWork dispose calls and reduces contention on the global workbuf
lists. And it's a lot faster on some benchmarks:
benchmark old ns/op new ns/op delta
BenchmarkBinaryTree17 11963668673 11206112763 -6.33%
BenchmarkFannkuch11 2643217136 2649182499 +0.23%
BenchmarkFmtFprintfEmpty 70.4 70.2 -0.28%
BenchmarkFmtFprintfString 364 307 -15.66%
BenchmarkFmtFprintfInt 317 282 -11.04%
BenchmarkFmtFprintfIntInt 512 483 -5.66%
BenchmarkFmtFprintfPrefixedInt 404 380 -5.94%
BenchmarkFmtFprintfFloat 521 479 -8.06%
BenchmarkFmtManyArgs 2164 1894 -12.48%
BenchmarkGobDecode 30366146 22429593 -26.14%
BenchmarkGobEncode 29867472 26663152 -10.73%
BenchmarkGzip 391236616 396779490 +1.42%
BenchmarkGunzip 96639491 96297024 -0.35%
BenchmarkHTTPClientServer 100110 70763 -29.31%
BenchmarkJSONEncode 51866051 52511382 +1.24%
BenchmarkJSONDecode 103813138 86094963 -17.07%
BenchmarkMandelbrot200 4121834 4120886 -0.02%
BenchmarkGoParse 16472789 5879949 -64.31%
BenchmarkRegexpMatchEasy0_32 140 140 +0.00%
BenchmarkRegexpMatchEasy0_1K 394 394 +0.00%
BenchmarkRegexpMatchEasy1_32 120 120 +0.00%
BenchmarkRegexpMatchEasy1_1K 621 614 -1.13%
BenchmarkRegexpMatchMedium_32 209 202 -3.35%
BenchmarkRegexpMatchMedium_1K 54889 55175 +0.52%
BenchmarkRegexpMatchHard_32 2682 2675 -0.26%
BenchmarkRegexpMatchHard_1K 79383 79524 +0.18%
BenchmarkRevcomp 584116718 584595320 +0.08%
BenchmarkTemplate 125400565 109620196 -12.58%
BenchmarkTimeParse 386 387 +0.26%
BenchmarkTimeFormat 580 447 -22.93%
(Best out of 10 runs. The delta of averages is similar.)
This also puts us in a good position to flush these caches when
nearing the end of concurrent marking, which will let us increase the
size of the work buffers while still controlling mark termination
pause time.
Change-Id: I2dd94c8517a19297a98ec280203cccaa58792522
Reviewed-on: https://go-review.googlesource.com/9178
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
2015-04-19 15:22:20 -04:00
|
|
|
|
2015-11-23 18:45:18 -05:00
|
|
|
gcw := &getg().m.p.ptr().gcw
|
runtime: avoid getfull() barrier most of the time
With the hybrid barrier, unless we're doing a STW GC or hit a very
rare race (~once per all.bash) that can start mark termination before
all of the work is drained, we don't need to drain the work queue at
all. Even draining an empty work queue is rather expensive since we
have to enter the getfull() barrier, so it's worth avoiding this.
Conveniently, it's quite easy to detect whether or not we actually
need the getufull() barrier: since the world is stopped when we enter
mark termination, everything must have flushed its work to the work
queue, so we can just check the queue. If the queue is empty and we
haven't queued up any jobs that may create more work (which should
always be the case with the hybrid barrier), we can simply have all GC
workers perform non-blocking drains.
Also conveniently, this solution is quite safe. If we do somehow screw
something up and there's work on the work queue, some worker will
still process it, it just may not happen in parallel.
This is not the "right" solution, but it's simple, expedient,
low-risk, and maintains compatibility with debug.gcrescanstacks. When
we remove the gcrescanstacks fallback in Go 1.9, we should also fix
the race that starts mark termination early, and then we can eliminate
work draining from mark termination.
Updates #17503.
Change-Id: I7b3cd5de6a248ab29d78c2b42aed8b7443641361
Reviewed-on: https://go-review.googlesource.com/32186
Reviewed-by: Rick Hudson <rlh@golang.org>
2016-10-26 17:05:41 -04:00
|
|
|
if work.helperDrainBlock {
|
|
|
|
|
gcDrain(gcw, gcDrainBlock)
|
|
|
|
|
} else {
|
|
|
|
|
gcDrain(gcw, gcDrainNoBlock)
|
|
|
|
|
}
|
runtime: switch to gcWork abstraction
This converts the garbage collector from directly manipulating work
buffers to using the new gcWork abstraction.
The previous management of work buffers was rather ad hoc. As a
result, switching to the gcWork abstraction changes many details of
work buffer management.
If greyobject fills a work buffer, it can now pull from work.partial
in addition to work.empty.
Previously, gcDrain started with a partial or empty work buffer and
fetched an empty work buffer if it filled its current buffer (in
greyobject). Now, gcDrain starts with a full work buffer and fetches
an partial or empty work buffer if it fills its current buffer (in
greyobject). The original behavior was bad because gcDrain would
immediately drop the empty work buffer returned by greyobject and
fetch a full work buffer, which greyobject was likely to immediately
overflow, fetching another empty work buffer, etc. The new behavior
isn't great at the start because greyobject is likely to immediately
overflow the full buffer, but the steady-state behavior should be more
stable. Both before and after this change, gcDrain fetches a full
work buffer if it drains its current buffer. Basically all of these
choices are bad; the right answer is to use a dual work buffer scheme.
Previously, shade always fetched a work buffer (though usually from
m.currentwbuf), even if the object was already marked. Now it only
fetches a work buffer if it actually greys an object.
Change-Id: I8b880ed660eb63135236fa5d5678f0c1c041881f
Reviewed-on: https://go-review.googlesource.com/5232
Reviewed-by: Russ Cox <rsc@golang.org>
Reviewed-by: Rick Hudson <rlh@golang.org>
2015-02-17 10:53:31 -05:00
|
|
|
gcw.dispose()
|
2014-11-11 17:05:02 -05:00
|
|
|
|
2016-03-14 13:51:23 -04:00
|
|
|
if debug.gccheckmark > 0 {
|
|
|
|
|
// This is expensive when there's a large number of
|
|
|
|
|
// Gs, so only do it if checkmark is also enabled.
|
|
|
|
|
gcMarkRootCheck()
|
|
|
|
|
}
|
2014-11-15 08:00:38 -05:00
|
|
|
if work.full != 0 {
|
2014-12-27 20:58:00 -08:00
|
|
|
throw("work.full != 0")
|
2014-11-15 08:00:38 -05:00
|
|
|
}
|
|
|
|
|
|
2014-11-11 17:05:02 -05:00
|
|
|
if work.nproc > 1 {
|
|
|
|
|
notesleep(&work.alldone)
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-15 18:24:06 -05:00
|
|
|
// Record that at least one root marking pass has completed.
|
|
|
|
|
work.markrootDone = true
|
runtime: scan objects with finalizers concurrently
This reduces pause time by ~25% relative to tip and by ~50% relative
to Go 1.5.1.
Currently one of the steps of STW mark termination is to loop (in
parallel) over all spans to find objects with finalizers in order to
mark all objects reachable from these objects and to treat the
finalizer special as a root. Unfortunately, even if there are no
finalizers at all, this loop takes roughly 1 ms/heap GB/core, so
multi-gigabyte heaps can quickly push our STW time past 10ms.
Fix this by moving this scan from mark termination to concurrent scan,
where it can run in parallel with mutators. The loop itself could also
be optimized, but this cost is small compared to concurrent marking.
Making this scan concurrent introduces two complications:
1) The scan currently walks the specials list of each span without
locking it, which is safe only with the world stopped. We fix this by
speculatively checking if a span has any specials (the vast majority
won't) and then locking the specials list only if there are specials
to check.
2) An object can have a finalizer set after concurrent scan, in which
case it won't have been marked appropriately by concurrent scan. If
the finalizer is a closure and is only reachable from the special, it
could be swept before it is run. Likewise, if the object is not marked
yet when the finalizer is set and then becomes unreachable before it
is marked, other objects reachable only from it may be swept before
the finalizer function is run. We fix this issue by making
addfinalizer ensure the same marking invariants as markroot does.
For multi-gigabyte heaps, this reduces max pause time by 20%–30%
relative to tip (depending on GOMAXPROCS) and by ~50% relative to Go
1.5.1 (where this loop was neither concurrent nor parallel). Here are
the results for the garbage benchmark:
---------------- max pause ----------------
Heap Procs Concurrent scan STW parallel scan 1.5.1
24GB 12 18ms 23ms 37ms
24GB 4 18ms 25ms 37ms
4GB 4 3.8ms 4.9ms 6.9ms
In all cases, 95%ile pause time is similar to the max pause time. This
also improves mean STW time by 10%–30%.
Fixes #11485.
Change-Id: I9359d8c3d120a51d23d924b52bf853a1299b1dfd
Reviewed-on: https://go-review.googlesource.com/14982
Reviewed-by: Rick Hudson <rlh@golang.org>
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2015-09-24 14:39:27 -04:00
|
|
|
|
2016-09-11 16:55:34 -04:00
|
|
|
// Double-check that all gcWork caches are empty. This should
|
|
|
|
|
// be ensured by mark 2 before we enter mark termination.
|
2017-06-13 12:01:56 -04:00
|
|
|
for _, p := range allp {
|
|
|
|
|
gcw := &p.gcw
|
2016-04-16 18:27:38 -04:00
|
|
|
if !gcw.empty() {
|
runtime: replace per-M workbuf cache with per-P gcWork cache
Currently, each M has a cache of the most recently used *workbuf. This
is used primarily by the write barrier so it doesn't have to access
the global workbuf lists on every write barrier. It's also used by
stack scanning because it's convenient.
This cache is important for write barrier performance, but this
particular approach has several downsides. It's faster than no cache,
but far from optimal (as the benchmarks below show). It's complex:
access to the cache is sprinkled through most of the workbuf list
operations and it requires special care to transform into and back out
of the gcWork cache that's actually used for scanning and marking. It
requires atomic exchanges to take ownership of the cached workbuf and
to return it to the M's cache even though it's almost always used by
only the current M. Since it's per-M, flushing these caches is O(# of
Ms), which may be high. And it has some significant subtleties: for
example, in general the cache shouldn't be used after the
harvestwbufs() in mark termination because it could hide work from
mark termination, but stack scanning can happen after this and *will*
use the cache (but it turns out this is okay because it will always be
followed by a getfull(), which drains the cache).
This change replaces this cache with a per-P gcWork object. This
gcWork cache can be used directly by scanning and marking (as long as
preemption is disabled, which is a general requirement of gcWork).
Since it's per-P, it doesn't require synchronization, which simplifies
things and means the only atomic operations in the write barrier are
occasionally fetching new work buffers and setting a mark bit if the
object isn't already marked. This cache can be flushed in O(# of Ps),
which is generally small. It follows a simple flushing rule: the cache
can be used during any phase, but during mark termination it must be
flushed before allowing preemption. This also makes the dispose during
mutator assist no longer necessary, which eliminates the vast majority
of gcWork dispose calls and reduces contention on the global workbuf
lists. And it's a lot faster on some benchmarks:
benchmark old ns/op new ns/op delta
BenchmarkBinaryTree17 11963668673 11206112763 -6.33%
BenchmarkFannkuch11 2643217136 2649182499 +0.23%
BenchmarkFmtFprintfEmpty 70.4 70.2 -0.28%
BenchmarkFmtFprintfString 364 307 -15.66%
BenchmarkFmtFprintfInt 317 282 -11.04%
BenchmarkFmtFprintfIntInt 512 483 -5.66%
BenchmarkFmtFprintfPrefixedInt 404 380 -5.94%
BenchmarkFmtFprintfFloat 521 479 -8.06%
BenchmarkFmtManyArgs 2164 1894 -12.48%
BenchmarkGobDecode 30366146 22429593 -26.14%
BenchmarkGobEncode 29867472 26663152 -10.73%
BenchmarkGzip 391236616 396779490 +1.42%
BenchmarkGunzip 96639491 96297024 -0.35%
BenchmarkHTTPClientServer 100110 70763 -29.31%
BenchmarkJSONEncode 51866051 52511382 +1.24%
BenchmarkJSONDecode 103813138 86094963 -17.07%
BenchmarkMandelbrot200 4121834 4120886 -0.02%
BenchmarkGoParse 16472789 5879949 -64.31%
BenchmarkRegexpMatchEasy0_32 140 140 +0.00%
BenchmarkRegexpMatchEasy0_1K 394 394 +0.00%
BenchmarkRegexpMatchEasy1_32 120 120 +0.00%
BenchmarkRegexpMatchEasy1_1K 621 614 -1.13%
BenchmarkRegexpMatchMedium_32 209 202 -3.35%
BenchmarkRegexpMatchMedium_1K 54889 55175 +0.52%
BenchmarkRegexpMatchHard_32 2682 2675 -0.26%
BenchmarkRegexpMatchHard_1K 79383 79524 +0.18%
BenchmarkRevcomp 584116718 584595320 +0.08%
BenchmarkTemplate 125400565 109620196 -12.58%
BenchmarkTimeParse 386 387 +0.26%
BenchmarkTimeFormat 580 447 -22.93%
(Best out of 10 runs. The delta of averages is similar.)
This also puts us in a good position to flush these caches when
nearing the end of concurrent marking, which will let us increase the
size of the work buffers while still controlling mark termination
pause time.
Change-Id: I2dd94c8517a19297a98ec280203cccaa58792522
Reviewed-on: https://go-review.googlesource.com/9178
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
2015-04-19 15:22:20 -04:00
|
|
|
throw("P has cached GC work at end of mark termination")
|
2016-04-16 18:27:38 -04:00
|
|
|
}
|
|
|
|
|
if gcw.scanWork != 0 || gcw.bytesMarked != 0 {
|
|
|
|
|
throw("P has unflushed stats at end of mark termination")
|
runtime: replace per-M workbuf cache with per-P gcWork cache
Currently, each M has a cache of the most recently used *workbuf. This
is used primarily by the write barrier so it doesn't have to access
the global workbuf lists on every write barrier. It's also used by
stack scanning because it's convenient.
This cache is important for write barrier performance, but this
particular approach has several downsides. It's faster than no cache,
but far from optimal (as the benchmarks below show). It's complex:
access to the cache is sprinkled through most of the workbuf list
operations and it requires special care to transform into and back out
of the gcWork cache that's actually used for scanning and marking. It
requires atomic exchanges to take ownership of the cached workbuf and
to return it to the M's cache even though it's almost always used by
only the current M. Since it's per-M, flushing these caches is O(# of
Ms), which may be high. And it has some significant subtleties: for
example, in general the cache shouldn't be used after the
harvestwbufs() in mark termination because it could hide work from
mark termination, but stack scanning can happen after this and *will*
use the cache (but it turns out this is okay because it will always be
followed by a getfull(), which drains the cache).
This change replaces this cache with a per-P gcWork object. This
gcWork cache can be used directly by scanning and marking (as long as
preemption is disabled, which is a general requirement of gcWork).
Since it's per-P, it doesn't require synchronization, which simplifies
things and means the only atomic operations in the write barrier are
occasionally fetching new work buffers and setting a mark bit if the
object isn't already marked. This cache can be flushed in O(# of Ps),
which is generally small. It follows a simple flushing rule: the cache
can be used during any phase, but during mark termination it must be
flushed before allowing preemption. This also makes the dispose during
mutator assist no longer necessary, which eliminates the vast majority
of gcWork dispose calls and reduces contention on the global workbuf
lists. And it's a lot faster on some benchmarks:
benchmark old ns/op new ns/op delta
BenchmarkBinaryTree17 11963668673 11206112763 -6.33%
BenchmarkFannkuch11 2643217136 2649182499 +0.23%
BenchmarkFmtFprintfEmpty 70.4 70.2 -0.28%
BenchmarkFmtFprintfString 364 307 -15.66%
BenchmarkFmtFprintfInt 317 282 -11.04%
BenchmarkFmtFprintfIntInt 512 483 -5.66%
BenchmarkFmtFprintfPrefixedInt 404 380 -5.94%
BenchmarkFmtFprintfFloat 521 479 -8.06%
BenchmarkFmtManyArgs 2164 1894 -12.48%
BenchmarkGobDecode 30366146 22429593 -26.14%
BenchmarkGobEncode 29867472 26663152 -10.73%
BenchmarkGzip 391236616 396779490 +1.42%
BenchmarkGunzip 96639491 96297024 -0.35%
BenchmarkHTTPClientServer 100110 70763 -29.31%
BenchmarkJSONEncode 51866051 52511382 +1.24%
BenchmarkJSONDecode 103813138 86094963 -17.07%
BenchmarkMandelbrot200 4121834 4120886 -0.02%
BenchmarkGoParse 16472789 5879949 -64.31%
BenchmarkRegexpMatchEasy0_32 140 140 +0.00%
BenchmarkRegexpMatchEasy0_1K 394 394 +0.00%
BenchmarkRegexpMatchEasy1_32 120 120 +0.00%
BenchmarkRegexpMatchEasy1_1K 621 614 -1.13%
BenchmarkRegexpMatchMedium_32 209 202 -3.35%
BenchmarkRegexpMatchMedium_1K 54889 55175 +0.52%
BenchmarkRegexpMatchHard_32 2682 2675 -0.26%
BenchmarkRegexpMatchHard_1K 79383 79524 +0.18%
BenchmarkRevcomp 584116718 584595320 +0.08%
BenchmarkTemplate 125400565 109620196 -12.58%
BenchmarkTimeParse 386 387 +0.26%
BenchmarkTimeFormat 580 447 -22.93%
(Best out of 10 runs. The delta of averages is similar.)
This also puts us in a good position to flush these caches when
nearing the end of concurrent marking, which will let us increase the
size of the work buffers while still controlling mark termination
pause time.
Change-Id: I2dd94c8517a19297a98ec280203cccaa58792522
Reviewed-on: https://go-review.googlesource.com/9178
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
2015-04-19 15:22:20 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-11 17:05:02 -05:00
|
|
|
cachestats()
|
runtime: introduce heap_live; replace use of heap_alloc in GC
Currently there are two main consumers of memstats.heap_alloc:
updatememstats (aka ReadMemStats) and shouldtriggergc.
updatememstats recomputes heap_alloc from the ground up, so we don't
need to keep heap_alloc up to date for it. shouldtriggergc wants to
know how many bytes were marked by the previous GC plus how many bytes
have been allocated since then, but this *isn't* what heap_alloc
tracks. heap_alloc also includes objects that are not marked and
haven't yet been swept.
Introduce a new memstat called heap_live that actually tracks what
shouldtriggergc wants to know and stop keeping heap_alloc up to date.
Unlike heap_alloc, heap_live follows a simple sawtooth that drops
during each mark termination and increases monotonically between GCs.
heap_alloc, on the other hand, has much more complicated behavior: it
may drop during sweep termination, slowly decreases from background
sweeping between GCs, is roughly unaffected by allocation as long as
there are unswept spans (because we sweep and allocate at the same
rate), and may go up after background sweeping is done depending on
the GC trigger.
heap_live simplifies computing next_gc and using it to figure out when
to trigger garbage collection. Currently, we guess next_gc at the end
of a cycle and update it as we sweep and get a better idea of how much
heap was marked. Now, since we're directly tracking how much heap is
marked, we can directly compute next_gc.
This also corrects bugs that could cause us to trigger GC early.
Currently, in any case where sweep termination actually finds spans to
sweep, heap_alloc is an overestimation of live heap, so we'll trigger
GC too early. heap_live, on the other hand, is unaffected by sweeping.
Change-Id: I1f96807b6ed60d4156e8173a8e68745ffc742388
Reviewed-on: https://go-review.googlesource.com/8389
Reviewed-by: Russ Cox <rsc@golang.org>
2015-03-30 18:01:32 -04:00
|
|
|
|
2016-09-15 14:30:31 -04:00
|
|
|
// Update the marked heap stat.
|
|
|
|
|
memstats.heap_marked = work.bytesMarked
|
runtime: use reachable heap estimate to set trigger/goal
Currently, we set the heap goal for the next GC cycle using the size
of the marked heap at the end of the current cycle. This can lead to a
bad feedback loop if the mutator is rapidly allocating and releasing
pointers that can significantly bloat heap size.
If the GC were STW, the marked heap size would be exactly the
reachable heap size (call it stwLive). However, in concurrent GC,
marked=stwLive+floatLive, where floatLive is the amount of "floating
garbage": objects that were reachable at some point during the cycle
and were marked, but which are no longer reachable by the end of the
cycle. If the GC cycle is short, then the mutator doesn't have much
time to create floating garbage, so marked≈stwLive. However, if the GC
cycle is long and the mutator is allocating and creating floating
garbage very rapidly, then it's possible that marked≫stwLive. Since
the runtime currently sets the heap goal based on marked, this will
cause it to set a high heap goal. This means that 1) the next GC cycle
will take longer because of the larger heap and 2) the assist ratio
will be low because of the large distance between the trigger and the
goal. The combination of these lets the mutator produce even more
floating garbage in the next cycle, which further exacerbates the
problem.
For example, on the garbage benchmark with GOMAXPROCS=1, this causes
the heap to grow to ~500MB and the garbage collector to retain upwards
of ~300MB of heap, while the true reachable heap size is ~32MB. This,
in turn, causes the GC cycle to take upwards of ~3 seconds.
Fix this bad feedback loop by estimating the true reachable heap size
(stwLive) and using this rather than the marked heap size
(stwLive+floatLive) as the basis for the GC trigger and heap goal.
This breaks the bad feedback loop and causes the mutator to assist
more, which decreases the rate at which it can create floating
garbage. On the same garbage benchmark, this reduces the maximum heap
size to ~73MB, the retained heap to ~40MB, and the duration of the GC
cycle to ~200ms.
Change-Id: I7712244c94240743b266f9eb720c03802799cdd1
Reviewed-on: https://go-review.googlesource.com/9177
Reviewed-by: Rick Hudson <rlh@golang.org>
2015-04-21 14:24:25 -04:00
|
|
|
|
runtime: fix (sometimes major) underestimation of heap_live
Currently, we update memstats.heap_live from mcache.local_cachealloc
whenever we lock the heap (e.g., to obtain a fresh span or to release
an unused span). However, under the right circumstances,
local_cachealloc can accumulate allocations up to the size of
the *entire heap* without flushing them to heap_live. Specifically,
since span allocations from an mcentral don't lock the heap, if a
large number of pages are held in an mcentral and the application
continues to use and free objects of that size class (e.g., the
BinaryTree17 benchmark), local_cachealloc won't be flushed until the
mcentral runs out of spans.
This is a problem because, unlike many of the memory statistics that
are purely informative, heap_live is used to determine when the
garbage collector should start and how hard it should work.
This commit eliminates local_cachealloc, instead atomically updating
heap_live directly. To control contention, we do this only when
obtaining a span from an mcentral. Furthermore, we make heap_live
conservative: allocating a span assumes that all free slots in that
span will be used and accounts for these when the span is
allocated, *before* the objects themselves are. This is important
because 1) this triggers the GC earlier than necessary rather than
potentially too late and 2) this leads to a conservative GC rate
rather than a GC rate that is potentially too low.
Alternatively, we could have flushed local_cachealloc when it passed
some threshold, but this would require determining a threshold and
would cause heap_live to underestimate the true value rather than
overestimate.
Fixes #12199.
name old time/op new time/op delta
BinaryTree17-12 2.88s ± 4% 2.88s ± 1% ~ (p=0.470 n=19+19)
Fannkuch11-12 2.48s ± 1% 2.48s ± 1% ~ (p=0.243 n=16+19)
FmtFprintfEmpty-12 50.9ns ± 2% 50.7ns ± 1% ~ (p=0.238 n=15+14)
FmtFprintfString-12 175ns ± 1% 171ns ± 1% -2.48% (p=0.000 n=18+18)
FmtFprintfInt-12 159ns ± 1% 158ns ± 1% -0.78% (p=0.000 n=19+18)
FmtFprintfIntInt-12 270ns ± 1% 265ns ± 2% -1.67% (p=0.000 n=18+18)
FmtFprintfPrefixedInt-12 235ns ± 1% 234ns ± 0% ~ (p=0.362 n=18+19)
FmtFprintfFloat-12 309ns ± 1% 308ns ± 1% -0.41% (p=0.001 n=18+19)
FmtManyArgs-12 1.10µs ± 1% 1.08µs ± 0% -1.96% (p=0.000 n=19+18)
GobDecode-12 7.81ms ± 1% 7.80ms ± 1% ~ (p=0.425 n=18+19)
GobEncode-12 6.53ms ± 1% 6.53ms ± 1% ~ (p=0.817 n=19+19)
Gzip-12 312ms ± 1% 312ms ± 2% ~ (p=0.967 n=19+20)
Gunzip-12 42.0ms ± 1% 41.9ms ± 1% ~ (p=0.172 n=19+19)
HTTPClientServer-12 63.7µs ± 1% 63.8µs ± 1% ~ (p=0.639 n=19+19)
JSONEncode-12 16.4ms ± 1% 16.4ms ± 1% ~ (p=0.954 n=19+19)
JSONDecode-12 58.5ms ± 1% 57.8ms ± 1% -1.27% (p=0.000 n=18+19)
Mandelbrot200-12 3.86ms ± 1% 3.88ms ± 0% +0.44% (p=0.000 n=18+18)
GoParse-12 3.67ms ± 2% 3.66ms ± 1% -0.52% (p=0.001 n=18+19)
RegexpMatchEasy0_32-12 100ns ± 1% 100ns ± 0% ~ (p=0.257 n=19+18)
RegexpMatchEasy0_1K-12 347ns ± 1% 347ns ± 1% ~ (p=0.527 n=18+18)
RegexpMatchEasy1_32-12 83.7ns ± 2% 83.1ns ± 2% ~ (p=0.096 n=18+19)
RegexpMatchEasy1_1K-12 509ns ± 1% 505ns ± 1% -0.75% (p=0.000 n=18+19)
RegexpMatchMedium_32-12 130ns ± 2% 129ns ± 1% ~ (p=0.962 n=20+20)
RegexpMatchMedium_1K-12 39.5µs ± 2% 39.4µs ± 1% ~ (p=0.376 n=20+19)
RegexpMatchHard_32-12 2.04µs ± 0% 2.04µs ± 1% ~ (p=0.195 n=18+17)
RegexpMatchHard_1K-12 61.4µs ± 1% 61.4µs ± 1% ~ (p=0.885 n=19+19)
Revcomp-12 540ms ± 2% 542ms ± 4% ~ (p=0.552 n=19+17)
Template-12 69.6ms ± 1% 71.2ms ± 1% +2.39% (p=0.000 n=20+20)
TimeParse-12 357ns ± 1% 357ns ± 1% ~ (p=0.883 n=18+20)
TimeFormat-12 379ns ± 1% 362ns ± 1% -4.53% (p=0.000 n=18+19)
[Geo mean] 62.0µs 61.8µs -0.44%
name old time/op new time/op delta
XBenchGarbage-12 5.89ms ± 2% 5.81ms ± 2% -1.41% (p=0.000 n=19+18)
Change-Id: I96b31cca6ae77c30693a891cff3fe663fa2447a0
Reviewed-on: https://go-review.googlesource.com/17748
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
2015-12-11 17:49:14 -05:00
|
|
|
// Update other GC heap size stats. This must happen after
|
|
|
|
|
// cachestats (which flushes local statistics to these) and
|
|
|
|
|
// flushallmcaches (which modifies heap_live).
|
runtime: fix underflow in next_gc calculation
Currently, it's possible for the next_gc calculation to underflow.
Since next_gc is unsigned, this wraps around and effectively disables
GC for the rest of the program's execution. Besides being obviously
wrong, this is causing test failures on 32-bit because some tests are
running out of heap.
This underflow happens for two reasons, both having to do with how we
estimate the reachable heap size at the end of the GC cycle.
One reason is that this calculation depends on the value of heap_live
at the beginning of the GC cycle, but we currently only record that
value during a concurrent GC and not during a forced STW GC. Fix this
by moving the recorded value from gcController to work and recording
it on a common code path.
The other reason is that we use the amount of allocation during the GC
cycle as an approximation of the amount of floating garbage and
subtract it from the marked heap to estimate the reachable heap.
However, since this is only an approximation, it's possible for the
amount of allocation during the cycle to be *larger* than the marked
heap size (since the runtime allocates white and it's possible for
these allocations to never be made reachable from the heap). Currently
this causes wrap-around in our estimate of the reachable heap size,
which in turn causes wrap-around in next_gc. Fix this by bottoming out
the reachable heap estimate at 0, in which case we just fall back to
triggering GC at heapminimum (which is okay since this only happens on
small heaps).
Fixes #10555, fixes #10556, and fixes #10559.
Change-Id: Iad07b529c03772356fede2ae557732f13ebfdb63
Reviewed-on: https://go-review.googlesource.com/9286
Run-TryBot: Austin Clements <austin@google.com>
Reviewed-by: Rick Hudson <rlh@golang.org>
2015-04-23 13:02:31 -04:00
|
|
|
memstats.heap_live = work.bytesMarked
|
2015-05-04 16:10:49 -04:00
|
|
|
memstats.heap_scan = uint64(gcController.scanWork)
|
2015-01-28 15:57:46 -05:00
|
|
|
|
2014-12-12 18:41:57 +01:00
|
|
|
if trace.enabled {
|
runtime: introduce heap_live; replace use of heap_alloc in GC
Currently there are two main consumers of memstats.heap_alloc:
updatememstats (aka ReadMemStats) and shouldtriggergc.
updatememstats recomputes heap_alloc from the ground up, so we don't
need to keep heap_alloc up to date for it. shouldtriggergc wants to
know how many bytes were marked by the previous GC plus how many bytes
have been allocated since then, but this *isn't* what heap_alloc
tracks. heap_alloc also includes objects that are not marked and
haven't yet been swept.
Introduce a new memstat called heap_live that actually tracks what
shouldtriggergc wants to know and stop keeping heap_alloc up to date.
Unlike heap_alloc, heap_live follows a simple sawtooth that drops
during each mark termination and increases monotonically between GCs.
heap_alloc, on the other hand, has much more complicated behavior: it
may drop during sweep termination, slowly decreases from background
sweeping between GCs, is roughly unaffected by allocation as long as
there are unswept spans (because we sweep and allocate at the same
rate), and may go up after background sweeping is done depending on
the GC trigger.
heap_live simplifies computing next_gc and using it to figure out when
to trigger garbage collection. Currently, we guess next_gc at the end
of a cycle and update it as we sweep and get a better idea of how much
heap was marked. Now, since we're directly tracking how much heap is
marked, we can directly compute next_gc.
This also corrects bugs that could cause us to trigger GC early.
Currently, in any case where sweep termination actually finds spans to
sweep, heap_alloc is an overestimation of live heap, so we'll trigger
GC too early. heap_live, on the other hand, is unaffected by sweeping.
Change-Id: I1f96807b6ed60d4156e8173a8e68745ffc742388
Reviewed-on: https://go-review.googlesource.com/8389
Reviewed-by: Russ Cox <rsc@golang.org>
2015-03-30 18:01:32 -04:00
|
|
|
traceHeapAlloc()
|
2014-12-12 18:41:57 +01:00
|
|
|
}
|
2015-02-19 16:43:27 -05:00
|
|
|
}
|
2014-11-11 17:05:02 -05:00
|
|
|
|
2015-09-24 14:30:09 -04:00
|
|
|
func gcSweep(mode gcMode) {
|
2015-03-05 17:33:08 -05:00
|
|
|
if gcphase != _GCoff {
|
|
|
|
|
throw("gcSweep being done but phase is not GCoff")
|
|
|
|
|
}
|
2014-11-15 08:00:38 -05:00
|
|
|
|
2015-02-19 16:21:42 -05:00
|
|
|
lock(&mheap_.lock)
|
2014-11-11 17:05:02 -05:00
|
|
|
mheap_.sweepgen += 2
|
|
|
|
|
mheap_.sweepdone = 0
|
runtime: make sweep time proportional to in-use spans
Currently sweeping walks the list of all spans, which means the work
in sweeping is proportional to the maximum number of spans ever used.
If the heap was once large but is now small, this causes an
amortization failure: on a small heap, GCs happen frequently, but a
full sweep still has to happen in each GC cycle, which means we spent
a lot of time in sweeping.
Fix this by creating a separate list consisting of just the in-use
spans to be swept, so sweeping is proportional to the number of in-use
spans (which is proportional to the live heap). Specifically, we
create two lists: a list of unswept in-use spans and a list of swept
in-use spans. At the start of the sweep cycle, the swept list becomes
the unswept list and the new swept list is empty. Allocating a new
in-use span adds it to the swept list. Sweeping moves spans from the
unswept list to the swept list.
This fixes the amortization problem because a shrinking heap moves
spans off the unswept list without adding them to the swept list,
reducing the time required by the next sweep cycle.
Updates #9265. This fix eliminates almost all of the time spent in
sweepone; however, markrootSpans has essentially the same bug, so now
the test program from this issue spends all of its time in
markrootSpans.
No significant effect on other benchmarks.
Change-Id: Ib382e82790aad907da1c127e62b3ab45d7a4ac1e
Reviewed-on: https://go-review.googlesource.com/30535
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rick Hudson <rlh@golang.org>
2016-10-05 17:50:39 -04:00
|
|
|
if mheap_.sweepSpans[mheap_.sweepgen/2%2].index != 0 {
|
|
|
|
|
// We should have drained this list during the last
|
|
|
|
|
// sweep phase. We certainly need to start this phase
|
|
|
|
|
// with an empty swept list.
|
|
|
|
|
throw("non-empty swept list")
|
|
|
|
|
}
|
2017-04-03 15:47:11 -04:00
|
|
|
mheap_.pagesSwept = 0
|
2014-11-11 17:05:02 -05:00
|
|
|
unlock(&mheap_.lock)
|
|
|
|
|
|
2015-02-19 16:43:27 -05:00
|
|
|
if !_ConcurrentSweep || mode == gcForceBlockMode {
|
|
|
|
|
// Special case synchronous sweep.
|
runtime: finish sweeping before concurrent GC starts
Currently, the concurrent sweep follows a 1:1 rule: when allocation
needs a span, it sweeps a span (likewise, when a large allocation
needs N pages, it sweeps until it frees N pages). This rule worked
well for the STW collector (especially when GOGC==100) because it did
no more sweeping than necessary to keep the heap from growing, would
generally finish sweeping just before GC, and ensured good temporal
locality between sweeping a page and allocating from it.
It doesn't work well with concurrent GC. Since concurrent GC requires
starting GC earlier (sometimes much earlier), the sweep often won't be
done when GC starts. Unfortunately, the first thing GC has to do is
finish the sweep. In the mean time, the mutator can continue
allocating, pushing the heap size even closer to the goal size. This
worked okay with the 7/8ths trigger, but it gets into a vicious cycle
with the GC trigger controller: if the mutator is allocating quickly
and driving the trigger lower, more and more sweep work will be left
to GC; this both causes GC to take longer (allowing the mutator to
allocate more during GC) and delays the start of the concurrent mark
phase, which throws off the GC controller's statistics and generally
causes it to push the trigger even lower.
As an example of a particularly bad case, the garbage benchmark with
GOMAXPROCS=4 and -benchmem 512 (MB) spends the first 0.4-0.8 seconds
of each GC cycle sweeping, during which the heap grows by between
109MB and 252MB.
To fix this, this change replaces the 1:1 sweep rule with a
proportional sweep rule. At the end of GC, GC knows exactly how much
heap allocation will occur before the next concurrent GC as well as
how many span pages must be swept. This change computes this "sweep
ratio" and when the mallocgc asks for a span, the mcentral sweeps
enough spans to bring the swept span count into ratio with the
allocated byte count.
On the benchmark from above, this entirely eliminates sweeping at the
beginning of GC, which reduces the time between startGC readying the
GC goroutine and GC stopping the world for sweep termination to ~100µs
during which the heap grows at most 134KB.
Change-Id: I35422d6bba0c2310d48bb1f8f30a72d29e98c1af
Reviewed-on: https://go-review.googlesource.com/8921
Reviewed-by: Rick Hudson <rlh@golang.org>
2015-04-13 23:34:57 -04:00
|
|
|
// Record that no proportional sweeping has to happen.
|
|
|
|
|
lock(&mheap_.lock)
|
|
|
|
|
mheap_.sweepPagesPerByte = 0
|
|
|
|
|
unlock(&mheap_.lock)
|
2014-11-11 17:05:02 -05:00
|
|
|
// Sweep all spans eagerly.
|
|
|
|
|
for sweepone() != ^uintptr(0) {
|
|
|
|
|
sweep.npausesweep++
|
|
|
|
|
}
|
2017-03-20 17:25:59 -04:00
|
|
|
// Free workbufs eagerly.
|
|
|
|
|
prepareFreeWorkbufs()
|
|
|
|
|
for freeSomeWbufs(false) {
|
|
|
|
|
}
|
2017-03-01 21:03:20 -05:00
|
|
|
// All "free" events for this mark/sweep cycle have
|
|
|
|
|
// now happened, so we can make this profile cycle
|
|
|
|
|
// available immediately.
|
2017-03-01 13:58:22 -05:00
|
|
|
mProf_NextCycle()
|
|
|
|
|
mProf_Flush()
|
2015-02-19 16:43:27 -05:00
|
|
|
return
|
2014-11-11 17:05:02 -05:00
|
|
|
}
|
|
|
|
|
|
2015-02-19 16:43:27 -05:00
|
|
|
// Background sweep.
|
|
|
|
|
lock(&sweep.lock)
|
2015-03-05 16:04:17 -05:00
|
|
|
if sweep.parked {
|
2015-02-19 16:43:27 -05:00
|
|
|
sweep.parked = false
|
2016-05-17 18:21:54 -04:00
|
|
|
ready(sweep.g, 0, true)
|
2015-02-19 16:43:27 -05:00
|
|
|
}
|
|
|
|
|
unlock(&sweep.lock)
|
|
|
|
|
}
|
2014-11-11 17:05:02 -05:00
|
|
|
|
2015-10-17 23:57:53 -04:00
|
|
|
// gcResetMarkState resets global state prior to marking (concurrent
|
2016-03-01 15:09:24 -05:00
|
|
|
// or STW) and resets the stack scan state of all Gs.
|
|
|
|
|
//
|
|
|
|
|
// This is safe to do without the world stopped because any Gs created
|
|
|
|
|
// during or after this will start out in the reset state.
|
2015-10-17 23:57:53 -04:00
|
|
|
func gcResetMarkState() {
|
2015-02-24 22:20:38 -05:00
|
|
|
// This may be called during a concurrent phase, so make sure
|
|
|
|
|
// allgs doesn't change.
|
|
|
|
|
lock(&allglock)
|
2015-02-24 22:29:33 -05:00
|
|
|
for _, gp := range allgs {
|
2015-06-16 19:20:18 -04:00
|
|
|
gp.gcscandone = false // set to true in gcphasework
|
2015-02-24 22:20:38 -05:00
|
|
|
gp.gcscanvalid = false // stack has not been scanned
|
runtime: directly track GC assist balance
Currently we track the per-G GC assist balance as two monotonically
increasing values: the bytes allocated by the G this cycle (gcalloc)
and the scan work performed by the G this cycle (gcscanwork). The
assist balance is hence assistRatio*gcalloc - gcscanwork.
This works, but has two important downsides:
1) It requires floating-point math to figure out if a G is in debt or
not. This makes it inappropriate to check for assist debt in the
hot path of mallocgc, so we only do this when a G allocates a new
span. As a result, Gs can operate "in the red", leading to
under-assist and extended GC cycle length.
2) Revising the assist ratio during a GC cycle can lead to an "assist
burst". If you think of plotting the scan work performed versus
heaps size, the assist ratio controls the slope of this line.
However, in the current system, the target line always passes
through 0 at the heap size that triggered GC, so if the runtime
increases the assist ratio, there has to be a potentially large
assist to jump from the current amount of scan work up to the new
target scan work for the current heap size.
This commit replaces this approach with directly tracking the GC
assist balance in terms of allocation credit bytes. Allocating N bytes
simply decreases this by N and assisting raises it by the amount of
scan work performed divided by the assist ratio (to get back to
bytes).
This will make it cheap to figure out if a G is in debt, which will
let us efficiently check if an assist is necessary *before* performing
an allocation and hence keep Gs "in the black".
This also fixes assist bursts because the assist ratio is now in terms
of *remaining* work, rather than work from the beginning of the GC
cycle. Hence, the plot of scan work versus heap size becomes
continuous: we can revise the slope, but this slope always starts from
where we are right now, rather than where we were at the beginning of
the cycle.
Change-Id: Ia821c5f07f8a433e8da7f195b52adfedd58bdf2c
Reviewed-on: https://go-review.googlesource.com/15408
Reviewed-by: Rick Hudson <rlh@golang.org>
2015-10-04 20:16:57 -07:00
|
|
|
gp.gcAssistBytes = 0
|
2015-02-24 22:20:38 -05:00
|
|
|
}
|
|
|
|
|
unlock(&allglock)
|
|
|
|
|
|
2015-06-26 13:56:58 -04:00
|
|
|
work.bytesMarked = 0
|
2017-04-21 11:45:44 -04:00
|
|
|
work.initialHeapLive = atomic.Load64(&memstats.heap_live)
|
2016-02-15 18:24:06 -05:00
|
|
|
work.markrootDone = false
|
2015-06-26 13:56:58 -04:00
|
|
|
}
|
|
|
|
|
|
2015-02-19 13:38:46 -05:00
|
|
|
// Hooks for other packages
|
2014-11-11 17:05:02 -05:00
|
|
|
|
2015-02-19 13:38:46 -05:00
|
|
|
var poolcleanup func()
|
|
|
|
|
|
|
|
|
|
//go:linkname sync_runtime_registerPoolCleanup sync.runtime_registerPoolCleanup
|
|
|
|
|
func sync_runtime_registerPoolCleanup(f func()) {
|
|
|
|
|
poolcleanup = f
|
2014-11-11 17:05:02 -05:00
|
|
|
}
|
|
|
|
|
|
2015-02-19 13:38:46 -05:00
|
|
|
func clearpools() {
|
|
|
|
|
// clear sync.Pools
|
|
|
|
|
if poolcleanup != nil {
|
|
|
|
|
poolcleanup()
|
2014-11-11 17:05:02 -05:00
|
|
|
}
|
|
|
|
|
|
2015-02-03 00:33:02 +03:00
|
|
|
// Clear central sudog cache.
|
|
|
|
|
// Leave per-P caches alone, they have strictly bounded size.
|
|
|
|
|
// Disconnect cached list before dropping it on the floor,
|
|
|
|
|
// so that a dangling ref to one entry does not pin all of them.
|
|
|
|
|
lock(&sched.sudoglock)
|
|
|
|
|
var sg, sgnext *sudog
|
|
|
|
|
for sg = sched.sudogcache; sg != nil; sg = sgnext {
|
|
|
|
|
sgnext = sg.next
|
|
|
|
|
sg.next = nil
|
|
|
|
|
}
|
|
|
|
|
sched.sudogcache = nil
|
|
|
|
|
unlock(&sched.sudoglock)
|
|
|
|
|
|
2015-02-05 13:35:41 +00:00
|
|
|
// Clear central defer pools.
|
|
|
|
|
// Leave per-P pools alone, they have strictly bounded size.
|
|
|
|
|
lock(&sched.deferlock)
|
|
|
|
|
for i := range sched.deferpool {
|
|
|
|
|
// disconnect cached list before dropping it on the floor,
|
|
|
|
|
// so that a dangling ref to one entry does not pin all of them.
|
|
|
|
|
var d, dlink *_defer
|
|
|
|
|
for d = sched.deferpool[i]; d != nil; d = dlink {
|
|
|
|
|
dlink = d.link
|
|
|
|
|
d.link = nil
|
|
|
|
|
}
|
|
|
|
|
sched.deferpool[i] = nil
|
|
|
|
|
}
|
|
|
|
|
unlock(&sched.deferlock)
|
2015-02-19 13:38:46 -05:00
|
|
|
}
|
|
|
|
|
|
2017-10-22 18:10:08 -04:00
|
|
|
// gchelper runs mark termination tasks on Ps other than the P
|
|
|
|
|
// coordinating mark termination.
|
|
|
|
|
//
|
|
|
|
|
// The caller is responsible for ensuring that this has a P to run on,
|
|
|
|
|
// even though it's running during STW. Because of this, it's allowed
|
|
|
|
|
// to have write barriers.
|
|
|
|
|
//
|
|
|
|
|
//go:yeswritebarrierrec
|
2015-02-19 13:38:46 -05:00
|
|
|
func gchelper() {
|
|
|
|
|
_g_ := getg()
|
|
|
|
|
_g_.m.traceback = 2
|
|
|
|
|
gchelperstart()
|
|
|
|
|
|
runtime: perform concurrent scan in GC workers
Currently the concurrent root scan is performed in its entirety by the
GC coordinator before entering concurrent mark (which enables GC
workers). This scan is done sequentially, which can prolong the scan
phase, delay the mark phase, and means that the scan phase does not
obey the 25% CPU goal. Furthermore, there's no need to complete the
root scan before starting marking (in fact, we already allow GC
assists to happen during the scan phase), so this acts as an
unnecessary barrier between root scanning and marking.
This change shifts the root scan work out of the GC coordinator and in
to the GC workers. The coordinator simply sets up the scan state and
enqueues the right number of root scan jobs. The GC workers then drain
the root scan jobs prior to draining heap scan jobs.
This parallelizes the root scan process, makes it obey the 25% CPU
goal, and effectively eliminates root scanning as an isolated phase,
allowing the system to smoothly transition from root scanning to heap
marking. This also eliminates a major non-STW responsibility of the GC
coordinator, which will make it easier to switch to a decentralized
state machine. Finally, it puts us in a good position to perform root
scanning in assists as well, which will help satisfy assists at the
beginning of the GC cycle.
This is mostly straightforward. One tricky aspect is that we have to
deal with preemption deadlock: where two non-preemptible gorountines
are trying to preempt each other to perform a stack scan. Given the
context where this happens, the only instance of this is two
background workers trying to scan each other. We avoid this by simply
not scanning the stacks of background workers during the concurrent
phase; this is safe because we'll scan them during mark termination
(and their stacks are *very* small and should not contain any new
pointers).
This change also switches the root marking during mark termination to
use the same gcDrain-based code path as concurrent mark. This
shouldn't affect performance because STW root marking was already
parallel and tasks switched to heap marking immediately when no more
root marking tasks were available. However, it simplifies the code and
unifies these code paths.
This has negligible effect on the go1 benchmarks. It slightly slows
down the garbage benchmark, possibly by making GC run slightly more
frequently.
name old time/op new time/op delta
XBenchGarbage-12 5.10ms ± 1% 5.24ms ± 1% +2.87% (p=0.000 n=18+18)
name old time/op new time/op delta
BinaryTree17-12 3.25s ± 3% 3.20s ± 5% -1.57% (p=0.013 n=20+20)
Fannkuch11-12 2.45s ± 1% 2.46s ± 1% +0.38% (p=0.019 n=20+18)
FmtFprintfEmpty-12 49.7ns ± 3% 49.9ns ± 4% ~ (p=0.851 n=19+20)
FmtFprintfString-12 170ns ± 2% 170ns ± 1% ~ (p=0.775 n=20+19)
FmtFprintfInt-12 161ns ± 1% 160ns ± 1% -0.78% (p=0.000 n=19+18)
FmtFprintfIntInt-12 267ns ± 1% 270ns ± 1% +1.04% (p=0.000 n=19+19)
FmtFprintfPrefixedInt-12 238ns ± 2% 238ns ± 1% ~ (p=0.133 n=18+19)
FmtFprintfFloat-12 311ns ± 1% 310ns ± 2% -0.35% (p=0.023 n=20+19)
FmtManyArgs-12 1.08µs ± 1% 1.06µs ± 1% -2.31% (p=0.000 n=20+20)
GobDecode-12 8.65ms ± 1% 8.63ms ± 1% ~ (p=0.377 n=18+20)
GobEncode-12 6.49ms ± 1% 6.52ms ± 1% +0.37% (p=0.015 n=20+20)
Gzip-12 319ms ± 3% 318ms ± 1% ~ (p=0.975 n=19+17)
Gunzip-12 41.9ms ± 1% 42.1ms ± 2% +0.65% (p=0.004 n=19+20)
HTTPClientServer-12 61.7µs ± 1% 62.6µs ± 1% +1.40% (p=0.000 n=18+20)
JSONEncode-12 16.8ms ± 1% 16.9ms ± 1% ~ (p=0.239 n=20+18)
JSONDecode-12 58.4ms ± 1% 60.7ms ± 1% +3.85% (p=0.000 n=19+20)
Mandelbrot200-12 3.86ms ± 0% 3.86ms ± 1% ~ (p=0.092 n=18+19)
GoParse-12 3.75ms ± 2% 3.75ms ± 2% ~ (p=0.708 n=19+20)
RegexpMatchEasy0_32-12 100ns ± 1% 100ns ± 2% +0.60% (p=0.010 n=17+20)
RegexpMatchEasy0_1K-12 341ns ± 1% 342ns ± 2% ~ (p=0.203 n=20+19)
RegexpMatchEasy1_32-12 82.5ns ± 2% 83.2ns ± 2% +0.83% (p=0.007 n=19+19)
RegexpMatchEasy1_1K-12 495ns ± 1% 495ns ± 2% ~ (p=0.970 n=19+18)
RegexpMatchMedium_32-12 130ns ± 2% 130ns ± 2% +0.59% (p=0.039 n=19+20)
RegexpMatchMedium_1K-12 39.2µs ± 1% 39.3µs ± 1% ~ (p=0.214 n=18+18)
RegexpMatchHard_32-12 2.03µs ± 2% 2.02µs ± 1% ~ (p=0.166 n=18+19)
RegexpMatchHard_1K-12 61.0µs ± 1% 60.9µs ± 1% ~ (p=0.169 n=20+18)
Revcomp-12 533ms ± 1% 535ms ± 1% ~ (p=0.071 n=19+17)
Template-12 68.1ms ± 2% 73.0ms ± 1% +7.26% (p=0.000 n=19+20)
TimeParse-12 355ns ± 2% 356ns ± 2% ~ (p=0.530 n=19+20)
TimeFormat-12 357ns ± 2% 347ns ± 1% -2.59% (p=0.000 n=20+19)
[Geo mean] 62.1µs 62.3µs +0.31%
name old speed new speed delta
GobDecode-12 88.7MB/s ± 1% 88.9MB/s ± 1% ~ (p=0.377 n=18+20)
GobEncode-12 118MB/s ± 1% 118MB/s ± 1% -0.37% (p=0.015 n=20+20)
Gzip-12 60.9MB/s ± 3% 60.9MB/s ± 1% ~ (p=0.944 n=19+17)
Gunzip-12 464MB/s ± 1% 461MB/s ± 2% -0.64% (p=0.004 n=19+20)
JSONEncode-12 115MB/s ± 1% 115MB/s ± 1% ~ (p=0.236 n=20+18)
JSONDecode-12 33.2MB/s ± 1% 32.0MB/s ± 1% -3.71% (p=0.000 n=19+20)
GoParse-12 15.5MB/s ± 2% 15.5MB/s ± 2% ~ (p=0.702 n=19+20)
RegexpMatchEasy0_32-12 320MB/s ± 1% 318MB/s ± 2% ~ (p=0.094 n=18+20)
RegexpMatchEasy0_1K-12 3.00GB/s ± 1% 2.99GB/s ± 1% ~ (p=0.194 n=20+19)
RegexpMatchEasy1_32-12 388MB/s ± 2% 385MB/s ± 2% -0.83% (p=0.008 n=19+19)
RegexpMatchEasy1_1K-12 2.07GB/s ± 1% 2.07GB/s ± 1% ~ (p=0.964 n=19+18)
RegexpMatchMedium_32-12 7.68MB/s ± 1% 7.64MB/s ± 2% -0.57% (p=0.020 n=19+20)
RegexpMatchMedium_1K-12 26.1MB/s ± 1% 26.1MB/s ± 1% ~ (p=0.211 n=18+18)
RegexpMatchHard_32-12 15.8MB/s ± 1% 15.8MB/s ± 1% ~ (p=0.180 n=18+19)
RegexpMatchHard_1K-12 16.8MB/s ± 1% 16.8MB/s ± 2% ~ (p=0.236 n=20+19)
Revcomp-12 477MB/s ± 1% 475MB/s ± 1% ~ (p=0.071 n=19+17)
Template-12 28.5MB/s ± 2% 26.6MB/s ± 1% -6.77% (p=0.000 n=19+20)
[Geo mean] 100MB/s 99.0MB/s -0.82%
Change-Id: I875bf6ceb306d1ee2f470cabf88aa6ede27c47a0
Reviewed-on: https://go-review.googlesource.com/16059
Reviewed-by: Rick Hudson <rlh@golang.org>
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2015-10-19 13:46:32 -04:00
|
|
|
// Parallel mark over GC roots and heap
|
|
|
|
|
if gcphase == _GCmarktermination {
|
2015-11-23 18:45:18 -05:00
|
|
|
gcw := &_g_.m.p.ptr().gcw
|
runtime: avoid getfull() barrier most of the time
With the hybrid barrier, unless we're doing a STW GC or hit a very
rare race (~once per all.bash) that can start mark termination before
all of the work is drained, we don't need to drain the work queue at
all. Even draining an empty work queue is rather expensive since we
have to enter the getfull() barrier, so it's worth avoiding this.
Conveniently, it's quite easy to detect whether or not we actually
need the getufull() barrier: since the world is stopped when we enter
mark termination, everything must have flushed its work to the work
queue, so we can just check the queue. If the queue is empty and we
haven't queued up any jobs that may create more work (which should
always be the case with the hybrid barrier), we can simply have all GC
workers perform non-blocking drains.
Also conveniently, this solution is quite safe. If we do somehow screw
something up and there's work on the work queue, some worker will
still process it, it just may not happen in parallel.
This is not the "right" solution, but it's simple, expedient,
low-risk, and maintains compatibility with debug.gcrescanstacks. When
we remove the gcrescanstacks fallback in Go 1.9, we should also fix
the race that starts mark termination early, and then we can eliminate
work draining from mark termination.
Updates #17503.
Change-Id: I7b3cd5de6a248ab29d78c2b42aed8b7443641361
Reviewed-on: https://go-review.googlesource.com/32186
Reviewed-by: Rick Hudson <rlh@golang.org>
2016-10-26 17:05:41 -04:00
|
|
|
if work.helperDrainBlock {
|
|
|
|
|
gcDrain(gcw, gcDrainBlock) // blocks in getfull
|
|
|
|
|
} else {
|
|
|
|
|
gcDrain(gcw, gcDrainNoBlock)
|
|
|
|
|
}
|
2015-02-19 13:38:46 -05:00
|
|
|
gcw.dispose()
|
|
|
|
|
}
|
2014-11-11 17:05:02 -05:00
|
|
|
|
2017-05-11 15:28:39 -04:00
|
|
|
nproc := atomic.Load(&work.nproc) // work.nproc can change right after we increment work.ndone
|
2015-11-02 14:09:24 -05:00
|
|
|
if atomic.Xadd(&work.ndone, +1) == nproc-1 {
|
2015-02-19 13:38:46 -05:00
|
|
|
notewakeup(&work.alldone)
|
|
|
|
|
}
|
|
|
|
|
_g_.m.traceback = 0
|
2014-11-11 17:05:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func gchelperstart() {
|
|
|
|
|
_g_ := getg()
|
|
|
|
|
|
|
|
|
|
if _g_.m.helpgc < 0 || _g_.m.helpgc >= _MaxGcproc {
|
2014-12-27 20:58:00 -08:00
|
|
|
throw("gchelperstart: bad m->helpgc")
|
2014-11-11 17:05:02 -05:00
|
|
|
}
|
|
|
|
|
if _g_ != _g_.m.g0 {
|
2014-12-27 20:58:00 -08:00
|
|
|
throw("gchelper not running on g0 stack")
|
2014-11-11 17:05:02 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-22 18:10:08 -04:00
|
|
|
// Timing
|
|
|
|
|
|
2015-03-26 18:48:42 -04:00
|
|
|
// itoaDiv formats val/(10**dec) into buf.
|
|
|
|
|
func itoaDiv(buf []byte, val uint64, dec int) []byte {
|
|
|
|
|
i := len(buf) - 1
|
|
|
|
|
idec := i - dec
|
|
|
|
|
for val >= 10 || i >= idec {
|
|
|
|
|
buf[i] = byte(val%10 + '0')
|
|
|
|
|
i--
|
|
|
|
|
if i == idec {
|
|
|
|
|
buf[i] = '.'
|
|
|
|
|
i--
|
|
|
|
|
}
|
|
|
|
|
val /= 10
|
|
|
|
|
}
|
|
|
|
|
buf[i] = byte(val + '0')
|
|
|
|
|
return buf[i:]
|
|
|
|
|
}
|
runtime: increase precision of gctrace times
Currently we truncate gctrace clock and CPU times to millisecond
precision. As a result, many phases are typically printed as 0, which
is fine for user consumption, but makes gathering statistics and
reports over GC traces difficult.
In 1.4, the gctrace line printed times in microseconds. This was
better for statistics, but not as easy for users to read or interpret,
and it generally made the trace lines longer.
This change strikes a balance between these extremes by printing
milliseconds, but including the decimal part to two significant
figures down to microsecond precision. This remains easy to read and
interpret, but includes more precision when it's useful.
For example, where the code currently prints,
gc #29 @1.629s 0%: 0+2+0+12+0 ms clock, 0+2+0+0/12/0+0 ms cpu, 4->4->2 MB, 4 MB goal, 1 P
this prints,
gc #29 @1.629s 0%: 0.005+2.1+0+12+0.29 ms clock, 0.005+2.1+0+0/12/0+0.29 ms cpu, 4->4->2 MB, 4 MB goal, 1 P
Fixes #10970.
Change-Id: I249624779433927cd8b0947b986df9060c289075
Reviewed-on: https://go-review.googlesource.com/10554
Reviewed-by: Russ Cox <rsc@golang.org>
2015-05-30 21:47:00 -04:00
|
|
|
|
|
|
|
|
// fmtNSAsMS nicely formats ns nanoseconds as milliseconds.
|
|
|
|
|
func fmtNSAsMS(buf []byte, ns uint64) []byte {
|
|
|
|
|
if ns >= 10e6 {
|
|
|
|
|
// Format as whole milliseconds.
|
|
|
|
|
return itoaDiv(buf, ns/1e6, 0)
|
|
|
|
|
}
|
|
|
|
|
// Format two digits of precision, with at most three decimal places.
|
|
|
|
|
x := ns / 1e3
|
|
|
|
|
if x == 0 {
|
|
|
|
|
buf[0] = '0'
|
|
|
|
|
return buf[:1]
|
|
|
|
|
}
|
|
|
|
|
dec := 3
|
|
|
|
|
for x >= 100 {
|
|
|
|
|
x /= 10
|
|
|
|
|
dec--
|
|
|
|
|
}
|
|
|
|
|
return itoaDiv(buf, x, dec)
|
|
|
|
|
}
|