• Austin Clements's avatar
    runtime: introduce heap_live; replace use of heap_alloc in GC · d7e0ad4b
    Austin Clements authored
    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/8389Reviewed-by: 's avatarRuss Cox <rsc@golang.org>
    d7e0ad4b
mstats.go 10.5 KB