1
0
mirror of https://github.com/golang/go synced 2024-10-04 04:31:21 -06:00
go/src/pkg/runtime/mgc0.c

2110 lines
58 KiB
C
Raw Normal View History

// 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.
runtime: concurrent GC sweep Moves sweep phase out of stoptheworld by adding background sweeper goroutine and lazy on-demand sweeping. It turned out to be somewhat trickier than I expected, because there is no point in time when we know size of live heap nor consistent number of mallocs and frees. So everything related to next_gc, mprof, memstats, etc becomes trickier. At the end of GC next_gc is conservatively set to heap_alloc*GOGC, which is much larger than real value. But after every sweep next_gc is decremented by freed*GOGC. So when everything is swept next_gc becomes what it should be. For mprof I had to introduce 3-generation scheme (allocs, revent_allocs, prev_allocs), because by the end of GC we know number of frees for the *previous* GC. Significant caution is required to not cross yet-unknown real value of next_gc. This is achieved by 2 means: 1. Whenever I allocate a span from MCentral, I sweep a span in that MCentral. 2. Whenever I allocate N pages from MHeap, I sweep until at least N pages are returned to heap. This provides quite strong guarantees that heap does not grow when it should now. http-1 allocated 7036 7033 -0.04% allocs 60 60 +0.00% cputime 51050 46700 -8.52% gc-pause-one 34060569 1777993 -94.78% gc-pause-total 2554 133 -94.79% latency-50 178448 170926 -4.22% latency-95 284350 198294 -30.26% latency-99 345191 220652 -36.08% rss 101564416 101007360 -0.55% sys-gc 6606832 6541296 -0.99% sys-heap 88801280 87752704 -1.18% sys-other 7334208 7405928 +0.98% sys-stack 524288 524288 +0.00% sys-total 103266608 102224216 -1.01% time 50339 46533 -7.56% virtual-mem 292990976 293728256 +0.25% garbage-1 allocated 2983818 2990889 +0.24% allocs 62880 62902 +0.03% cputime 16480000 16190000 -1.76% gc-pause-one 828462467 487875135 -41.11% gc-pause-total 4142312 2439375 -41.11% rss 1151709184 1153712128 +0.17% sys-gc 66068352 66068352 +0.00% sys-heap 1039728640 1039728640 +0.00% sys-other 37776064 40770176 +7.93% sys-stack 8781824 8781824 +0.00% sys-total 1152354880 1155348992 +0.26% time 16496998 16199876 -1.80% virtual-mem 1409564672 1402281984 -0.52% LGTM=rsc R=golang-codereviews, sameer, rsc, iant, jeremyjackins, gobot CC=golang-codereviews, khr https://golang.org/cl/46430043
2014-02-12 11:16:42 -07:00
// Garbage collector (GC).
//
// GC is:
// - mark&sweep
// - mostly precise (with the exception of some C-allocated objects, assembly frames/arguments, etc)
// - parallel (up to MaxGcproc threads)
// - partially concurrent (mark is stop-the-world, while sweep is concurrent)
// - non-moving/non-compacting
// - full (non-partial)
//
// 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).
//
// Concurrent sweep.
// 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).
// However, at the end of the stop-the-world GC phase we don't know the size of the live heap,
// and so next_gc calculation is tricky and happens as follows.
// At the end of the stop-the-world phase next_gc is conservatively set based on total
// heap size; all spans are marked as "needs sweeping".
// Whenever a span is swept, next_gc is decremented by GOGC*newly_freed_memory.
// The background sweeper goroutine simply sweeps spans one-by-one bringing next_gc
// closer to the target value. However, this is not enough to avoid over-allocating memory.
// Consider that a goroutine wants to allocate a new span for a large object and
// there are no free swept spans, but there are small-object unswept spans.
// If the goroutine naively allocates a new span, it can surpass the yet-unknown
// target next_gc value. In order to prevent such cases (1) 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; (2) when a goroutine needs to
// allocate large-object span from heap, it sweeps spans until it frees at least
// that many pages into heap. Together these two measures ensure that we don't surpass
// target next_gc value by a large margin. There is an exception: 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.
// 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).
#include "runtime.h"
#include "arch_GOARCH.h"
#include "malloc.h"
runtime: stack split + garbage collection bug The g->sched.sp saved stack pointer and the g->stackbase and g->stackguard stack bounds can change even while "the world is stopped", because a goroutine has to call functions (and therefore might split its stack) when exiting a system call to check whether the world is stopped (and if so, wait until the world continues). That means the garbage collector cannot access those values safely (without a race) for goroutines executing system calls. Instead, save a consistent triple in g->gcsp, g->gcstack, g->gcguard during entersyscall and have the garbage collector refer to those. The old code was occasionally seeing (because of the race) an sp and stk that did not correspond to each other, so that stk - sp was not the number of stack bytes following sp. In that case, if sp < stk then the call scanblock(sp, stk - sp) scanned too many bytes (anything between the two pointers, which pointed into different allocation blocks). If sp > stk then stk - sp wrapped around. On 32-bit, stk - sp is a uintptr (uint32) converted to int64 in the call to scanblock, so a large (~4G) but positive number. Scanblock would try to scan that many bytes and eventually fault accessing unmapped memory. On 64-bit, stk - sp is a uintptr (uint64) promoted to int64 in the call to scanblock, so a negative number. Scanblock would not scan anything, possibly causing in-use blocks to be freed. In short, 32-bit platforms would have seen either ineffective garbage collection or crashes during garbage collection, while 64-bit platforms would have seen either ineffective or incorrect garbage collection. You can see the invalid arguments to scanblock in the stack traces in issue 1620. Fixes #1620. Fixes #1746. R=iant, r CC=golang-dev https://golang.org/cl/4437075
2011-04-27 21:21:12 -06:00
#include "stack.h"
#include "mgc0.h"
reflect, runtime: fix crash in GC due to reflect.call + precise GC Given type Outer struct { *Inner ... } the compiler generates the implementation of (*Outer).M dispatching to the embedded Inner. The implementation is logically: func (p *Outer) M() { (p.Inner).M() } but since the only change here is the replacement of one pointer receiver with another, the actual generated code overwrites the original receiver with the p.Inner pointer and then jumps to the M method expecting the *Inner receiver. During reflect.Value.Call, we create an argument frame and the associated data structures to describe it to the garbage collector, populate the frame, call reflect.call to run a function call using that frame, and then copy the results back out of the frame. The reflect.call function does a memmove of the frame structure onto the stack (to set up the inputs), runs the call, and the memmoves the stack back to the frame structure (to preserve the outputs). Originally reflect.call did not distinguish inputs from outputs: both memmoves were for the full stack frame. However, in the case where the called function was one of these wrappers, the rewritten receiver is almost certainly a different type than the original receiver. This is not a problem on the stack, where we use the program counter to determine the type information and understand that during (*Outer).M the receiver is an *Outer while during (*Inner).M the receiver in the same memory word is now an *Inner. But in the statically typed argument frame created by reflect, the receiver is always an *Outer. Copying the modified receiver pointer off the stack into the frame will store an *Inner there, and then if a garbage collection happens to scan that argument frame before it is discarded, it will scan the *Inner memory as if it were an *Outer. If the two have different memory layouts, the collection will intepret the memory incorrectly. Fix by only copying back the results. Fixes #7725. LGTM=khr R=khr CC=dave, golang-codereviews https://golang.org/cl/85180043
2014-04-08 09:11:35 -06:00
#include "chan.h"
#include "race.h"
#include "type.h"
#include "typekind.h"
#include "funcdata.h"
#include "../../cmd/ld/textflag.h"
enum {
Debug = 0,
ConcurrentSweep = 1,
PreciseScan = 1,
WorkbufSize = 4*1024,
2014-01-30 02:28:19 -07:00
FinBlockSize = 4*1024,
RootData = 0,
RootBss = 1,
RootFinalizers = 2,
RootSpans = 3,
RootFlushCaches = 4,
RootCount = 5,
};
#define ScanConservatively ((byte*)1)
runtime: concurrent GC sweep Moves sweep phase out of stoptheworld by adding background sweeper goroutine and lazy on-demand sweeping. It turned out to be somewhat trickier than I expected, because there is no point in time when we know size of live heap nor consistent number of mallocs and frees. So everything related to next_gc, mprof, memstats, etc becomes trickier. At the end of GC next_gc is conservatively set to heap_alloc*GOGC, which is much larger than real value. But after every sweep next_gc is decremented by freed*GOGC. So when everything is swept next_gc becomes what it should be. For mprof I had to introduce 3-generation scheme (allocs, revent_allocs, prev_allocs), because by the end of GC we know number of frees for the *previous* GC. Significant caution is required to not cross yet-unknown real value of next_gc. This is achieved by 2 means: 1. Whenever I allocate a span from MCentral, I sweep a span in that MCentral. 2. Whenever I allocate N pages from MHeap, I sweep until at least N pages are returned to heap. This provides quite strong guarantees that heap does not grow when it should now. http-1 allocated 7036 7033 -0.04% allocs 60 60 +0.00% cputime 51050 46700 -8.52% gc-pause-one 34060569 1777993 -94.78% gc-pause-total 2554 133 -94.79% latency-50 178448 170926 -4.22% latency-95 284350 198294 -30.26% latency-99 345191 220652 -36.08% rss 101564416 101007360 -0.55% sys-gc 6606832 6541296 -0.99% sys-heap 88801280 87752704 -1.18% sys-other 7334208 7405928 +0.98% sys-stack 524288 524288 +0.00% sys-total 103266608 102224216 -1.01% time 50339 46533 -7.56% virtual-mem 292990976 293728256 +0.25% garbage-1 allocated 2983818 2990889 +0.24% allocs 62880 62902 +0.03% cputime 16480000 16190000 -1.76% gc-pause-one 828462467 487875135 -41.11% gc-pause-total 4142312 2439375 -41.11% rss 1151709184 1153712128 +0.17% sys-gc 66068352 66068352 +0.00% sys-heap 1039728640 1039728640 +0.00% sys-other 37776064 40770176 +7.93% sys-stack 8781824 8781824 +0.00% sys-total 1152354880 1155348992 +0.26% time 16496998 16199876 -1.80% virtual-mem 1409564672 1402281984 -0.52% LGTM=rsc R=golang-codereviews, sameer, rsc, iant, jeremyjackins, gobot CC=golang-codereviews, khr https://golang.org/cl/46430043
2014-02-12 11:16:42 -07:00
// Initialized from $GOGC. GOGC=off means no gc.
extern int32 runtime·gcpercent;
runtime: concurrent GC sweep Moves sweep phase out of stoptheworld by adding background sweeper goroutine and lazy on-demand sweeping. It turned out to be somewhat trickier than I expected, because there is no point in time when we know size of live heap nor consistent number of mallocs and frees. So everything related to next_gc, mprof, memstats, etc becomes trickier. At the end of GC next_gc is conservatively set to heap_alloc*GOGC, which is much larger than real value. But after every sweep next_gc is decremented by freed*GOGC. So when everything is swept next_gc becomes what it should be. For mprof I had to introduce 3-generation scheme (allocs, revent_allocs, prev_allocs), because by the end of GC we know number of frees for the *previous* GC. Significant caution is required to not cross yet-unknown real value of next_gc. This is achieved by 2 means: 1. Whenever I allocate a span from MCentral, I sweep a span in that MCentral. 2. Whenever I allocate N pages from MHeap, I sweep until at least N pages are returned to heap. This provides quite strong guarantees that heap does not grow when it should now. http-1 allocated 7036 7033 -0.04% allocs 60 60 +0.00% cputime 51050 46700 -8.52% gc-pause-one 34060569 1777993 -94.78% gc-pause-total 2554 133 -94.79% latency-50 178448 170926 -4.22% latency-95 284350 198294 -30.26% latency-99 345191 220652 -36.08% rss 101564416 101007360 -0.55% sys-gc 6606832 6541296 -0.99% sys-heap 88801280 87752704 -1.18% sys-other 7334208 7405928 +0.98% sys-stack 524288 524288 +0.00% sys-total 103266608 102224216 -1.01% time 50339 46533 -7.56% virtual-mem 292990976 293728256 +0.25% garbage-1 allocated 2983818 2990889 +0.24% allocs 62880 62902 +0.03% cputime 16480000 16190000 -1.76% gc-pause-one 828462467 487875135 -41.11% gc-pause-total 4142312 2439375 -41.11% rss 1151709184 1153712128 +0.17% sys-gc 66068352 66068352 +0.00% sys-heap 1039728640 1039728640 +0.00% sys-other 37776064 40770176 +7.93% sys-stack 8781824 8781824 +0.00% sys-total 1152354880 1155348992 +0.26% time 16496998 16199876 -1.80% virtual-mem 1409564672 1402281984 -0.52% LGTM=rsc R=golang-codereviews, sameer, rsc, iant, jeremyjackins, gobot CC=golang-codereviews, khr https://golang.org/cl/46430043
2014-02-12 11:16:42 -07:00
static FuncVal* poolcleanup;
void
sync·runtime_registerPoolCleanup(FuncVal *f)
{
poolcleanup = f;
}
void
runtime·clearpools(void)
{
P *p, **pp;
MCache *c;
int32 i;
// clear sync.Pool's
if(poolcleanup != nil)
reflect·call(poolcleanup, nil, 0, 0);
for(pp=runtime·allp; p=*pp; pp++) {
// clear tinyalloc pool
c = p->mcache;
if(c != nil) {
c->tiny = nil;
c->tinysize = 0;
}
// clear defer pools
for(i=0; i<nelem(p->deferpool); i++)
p->deferpool[i] = nil;
}
}
// Holding worldsema grants an M the right to try to stop the world.
// The procedure is:
//
// runtime·semacquire(&runtime·worldsema);
// m->gcing = 1;
// runtime·stoptheworld();
//
// ... do stuff ...
//
// m->gcing = 0;
// runtime·semrelease(&runtime·worldsema);
// runtime·starttheworld();
//
uint32 runtime·worldsema = 1;
typedef struct Workbuf Workbuf;
struct Workbuf
{
LFNode node; // must be first
uintptr nobj;
byte* obj[(WorkbufSize-sizeof(LFNode)-sizeof(uintptr))/PtrSize];
};
typedef struct Finalizer Finalizer;
struct Finalizer
{
FuncVal *fn;
void *arg;
uintptr nret;
Type *fint;
PtrType *ot;
};
typedef struct FinBlock FinBlock;
struct FinBlock
{
FinBlock *alllink;
FinBlock *next;
int32 cnt;
int32 cap;
Finalizer fin[1];
};
extern byte data[];
extern byte edata[];
extern byte bss[];
extern byte ebss[];
extern byte gcdata[];
extern byte gcbss[];
static Lock finlock; // protects the following variables
static FinBlock *finq; // list of finalizers that are to be executed
static FinBlock *finc; // cache of free blocks
static FinBlock *allfin; // list of all blocks
bool runtime·fingwait;
bool runtime·fingwake;
byte* runtime·gcdatamask;
byte* runtime·gcbssmask;
runtime: concurrent GC sweep Moves sweep phase out of stoptheworld by adding background sweeper goroutine and lazy on-demand sweeping. It turned out to be somewhat trickier than I expected, because there is no point in time when we know size of live heap nor consistent number of mallocs and frees. So everything related to next_gc, mprof, memstats, etc becomes trickier. At the end of GC next_gc is conservatively set to heap_alloc*GOGC, which is much larger than real value. But after every sweep next_gc is decremented by freed*GOGC. So when everything is swept next_gc becomes what it should be. For mprof I had to introduce 3-generation scheme (allocs, revent_allocs, prev_allocs), because by the end of GC we know number of frees for the *previous* GC. Significant caution is required to not cross yet-unknown real value of next_gc. This is achieved by 2 means: 1. Whenever I allocate a span from MCentral, I sweep a span in that MCentral. 2. Whenever I allocate N pages from MHeap, I sweep until at least N pages are returned to heap. This provides quite strong guarantees that heap does not grow when it should now. http-1 allocated 7036 7033 -0.04% allocs 60 60 +0.00% cputime 51050 46700 -8.52% gc-pause-one 34060569 1777993 -94.78% gc-pause-total 2554 133 -94.79% latency-50 178448 170926 -4.22% latency-95 284350 198294 -30.26% latency-99 345191 220652 -36.08% rss 101564416 101007360 -0.55% sys-gc 6606832 6541296 -0.99% sys-heap 88801280 87752704 -1.18% sys-other 7334208 7405928 +0.98% sys-stack 524288 524288 +0.00% sys-total 103266608 102224216 -1.01% time 50339 46533 -7.56% virtual-mem 292990976 293728256 +0.25% garbage-1 allocated 2983818 2990889 +0.24% allocs 62880 62902 +0.03% cputime 16480000 16190000 -1.76% gc-pause-one 828462467 487875135 -41.11% gc-pause-total 4142312 2439375 -41.11% rss 1151709184 1153712128 +0.17% sys-gc 66068352 66068352 +0.00% sys-heap 1039728640 1039728640 +0.00% sys-other 37776064 40770176 +7.93% sys-stack 8781824 8781824 +0.00% sys-total 1152354880 1155348992 +0.26% time 16496998 16199876 -1.80% virtual-mem 1409564672 1402281984 -0.52% LGTM=rsc R=golang-codereviews, sameer, rsc, iant, jeremyjackins, gobot CC=golang-codereviews, khr https://golang.org/cl/46430043
2014-02-12 11:16:42 -07:00
static Lock gclock;
runtime: concurrent GC sweep Moves sweep phase out of stoptheworld by adding background sweeper goroutine and lazy on-demand sweeping. It turned out to be somewhat trickier than I expected, because there is no point in time when we know size of live heap nor consistent number of mallocs and frees. So everything related to next_gc, mprof, memstats, etc becomes trickier. At the end of GC next_gc is conservatively set to heap_alloc*GOGC, which is much larger than real value. But after every sweep next_gc is decremented by freed*GOGC. So when everything is swept next_gc becomes what it should be. For mprof I had to introduce 3-generation scheme (allocs, revent_allocs, prev_allocs), because by the end of GC we know number of frees for the *previous* GC. Significant caution is required to not cross yet-unknown real value of next_gc. This is achieved by 2 means: 1. Whenever I allocate a span from MCentral, I sweep a span in that MCentral. 2. Whenever I allocate N pages from MHeap, I sweep until at least N pages are returned to heap. This provides quite strong guarantees that heap does not grow when it should now. http-1 allocated 7036 7033 -0.04% allocs 60 60 +0.00% cputime 51050 46700 -8.52% gc-pause-one 34060569 1777993 -94.78% gc-pause-total 2554 133 -94.79% latency-50 178448 170926 -4.22% latency-95 284350 198294 -30.26% latency-99 345191 220652 -36.08% rss 101564416 101007360 -0.55% sys-gc 6606832 6541296 -0.99% sys-heap 88801280 87752704 -1.18% sys-other 7334208 7405928 +0.98% sys-stack 524288 524288 +0.00% sys-total 103266608 102224216 -1.01% time 50339 46533 -7.56% virtual-mem 292990976 293728256 +0.25% garbage-1 allocated 2983818 2990889 +0.24% allocs 62880 62902 +0.03% cputime 16480000 16190000 -1.76% gc-pause-one 828462467 487875135 -41.11% gc-pause-total 4142312 2439375 -41.11% rss 1151709184 1153712128 +0.17% sys-gc 66068352 66068352 +0.00% sys-heap 1039728640 1039728640 +0.00% sys-other 37776064 40770176 +7.93% sys-stack 8781824 8781824 +0.00% sys-total 1152354880 1155348992 +0.26% time 16496998 16199876 -1.80% virtual-mem 1409564672 1402281984 -0.52% LGTM=rsc R=golang-codereviews, sameer, rsc, iant, jeremyjackins, gobot CC=golang-codereviews, khr https://golang.org/cl/46430043
2014-02-12 11:16:42 -07:00
static void runfinq(void);
static void bgsweep(void);
static Workbuf* getempty(Workbuf*);
static Workbuf* getfull(Workbuf*);
static void putempty(Workbuf*);
static Workbuf* handoff(Workbuf*);
static void gchelperstart(void);
static void flushallmcaches(void);
static bool scanframe(Stkframe *frame, void *unused);
static void scanstack(G *gp);
static byte* unrollglobgcprog(byte *prog, uintptr size);
runtime: concurrent GC sweep Moves sweep phase out of stoptheworld by adding background sweeper goroutine and lazy on-demand sweeping. It turned out to be somewhat trickier than I expected, because there is no point in time when we know size of live heap nor consistent number of mallocs and frees. So everything related to next_gc, mprof, memstats, etc becomes trickier. At the end of GC next_gc is conservatively set to heap_alloc*GOGC, which is much larger than real value. But after every sweep next_gc is decremented by freed*GOGC. So when everything is swept next_gc becomes what it should be. For mprof I had to introduce 3-generation scheme (allocs, revent_allocs, prev_allocs), because by the end of GC we know number of frees for the *previous* GC. Significant caution is required to not cross yet-unknown real value of next_gc. This is achieved by 2 means: 1. Whenever I allocate a span from MCentral, I sweep a span in that MCentral. 2. Whenever I allocate N pages from MHeap, I sweep until at least N pages are returned to heap. This provides quite strong guarantees that heap does not grow when it should now. http-1 allocated 7036 7033 -0.04% allocs 60 60 +0.00% cputime 51050 46700 -8.52% gc-pause-one 34060569 1777993 -94.78% gc-pause-total 2554 133 -94.79% latency-50 178448 170926 -4.22% latency-95 284350 198294 -30.26% latency-99 345191 220652 -36.08% rss 101564416 101007360 -0.55% sys-gc 6606832 6541296 -0.99% sys-heap 88801280 87752704 -1.18% sys-other 7334208 7405928 +0.98% sys-stack 524288 524288 +0.00% sys-total 103266608 102224216 -1.01% time 50339 46533 -7.56% virtual-mem 292990976 293728256 +0.25% garbage-1 allocated 2983818 2990889 +0.24% allocs 62880 62902 +0.03% cputime 16480000 16190000 -1.76% gc-pause-one 828462467 487875135 -41.11% gc-pause-total 4142312 2439375 -41.11% rss 1151709184 1153712128 +0.17% sys-gc 66068352 66068352 +0.00% sys-heap 1039728640 1039728640 +0.00% sys-other 37776064 40770176 +7.93% sys-stack 8781824 8781824 +0.00% sys-total 1152354880 1155348992 +0.26% time 16496998 16199876 -1.80% virtual-mem 1409564672 1402281984 -0.52% LGTM=rsc R=golang-codereviews, sameer, rsc, iant, jeremyjackins, gobot CC=golang-codereviews, khr https://golang.org/cl/46430043
2014-02-12 11:16:42 -07:00
static FuncVal runfinqv = {runfinq};
static FuncVal bgsweepv = {bgsweep};
static struct {
runtime: faster GC mark phase Also bump MaxGcproc to 8. benchmark old ns/op new ns/op delta Parser 3796323000 3763880000 -0.85% Parser-2 3591752500 3518560250 -2.04% Parser-4 3423825250 3334955250 -2.60% Parser-8 3304585500 3267014750 -1.14% Parser-16 3313615750 3286160500 -0.83% Tree 984128500 942501166 -4.23% Tree-2 932564444 883266222 -5.29% Tree-4 835831000 799912777 -4.30% Tree-8 819238500 789717333 -3.73% Tree-16 880837833 837840055 -5.13% Tree2 604698100 579716900 -4.13% Tree2-2 372414500 356765200 -4.20% Tree2-4 187488100 177455900 -5.56% Tree2-8 136315300 102086700 -25.11% Tree2-16 93725900 76705800 -22.18% ParserPause 157441210 166202783 +5.56% ParserPause-2 93842650 85199900 -9.21% ParserPause-4 56844404 53535684 -5.82% ParserPause-8 35739446 30767613 -16.15% ParserPause-16 32718255 27212441 -16.83% TreePause 29610557 29787725 +0.60% TreePause-2 24001659 20674421 -13.86% TreePause-4 15114887 12842781 -15.03% TreePause-8 13128725 10741747 -22.22% TreePause-16 16131360 12506901 -22.47% Tree2Pause 2673350920 2651045280 -0.83% Tree2Pause-2 1796999200 1709350040 -4.88% Tree2Pause-4 1163553320 1090706480 -6.67% Tree2Pause-8 987032520 858916360 -25.11% Tree2Pause-16 864758560 809567480 -6.81% ParserLastPause 280537000 289047000 +3.03% ParserLastPause-2 183030000 166748000 -8.90% ParserLastPause-4 105817000 91552000 -13.48% ParserLastPause-8 65127000 53288000 -18.18% ParserLastPause-16 45258000 38334000 -15.30% TreeLastPause 45072000 51449000 +12.39% TreeLastPause-2 39269000 37866000 -3.57% TreeLastPause-4 23564000 20649000 -12.37% TreeLastPause-8 20881000 15807000 -24.30% TreeLastPause-16 23297000 17309000 -25.70% Tree2LastPause 6046912000 5797120000 -4.13% Tree2LastPause-2 3724034000 3567592000 -4.20% Tree2LastPause-4 1874831000 1774524000 -5.65% Tree2LastPause-8 1363108000 1020809000 -12.79% Tree2LastPause-16 937208000 767019000 -22.18% R=rsc, 0xe2.0x9a.0x9b CC=golang-dev https://golang.org/cl/6223050
2012-05-24 00:55:50 -06:00
uint64 full; // lock-free list of full blocks
uint64 empty; // lock-free list of empty blocks
byte pad0[CacheLineSize]; // prevents false-sharing between full/empty and nproc/nwait
uint32 nproc;
int64 tstart;
volatile uint32 nwait;
volatile uint32 ndone;
Note alldone;
ParFor* markfor;
} work;
// scanblock scans a block of n bytes starting at pointer b for references
// to other objects, scanning any it finds recursively until there are no
// unscanned objects left. Instead of using an explicit recursion, it keeps
// a work list in the Workbuf* structures and loops in the main function
// body. Keeping an explicit work list is easier on the stack allocator and
// more efficient.
static void
scanblock(byte *b, uintptr n, byte *ptrmask)
{
byte *obj, *p, *arena_start, *arena_used, **wp, *scanbuf[8], *ptrbitp, *bitp, bits, xbits, shift, cached;
uintptr i, nobj, size, idx, x, off, scanbufpos;
intptr ncached;
Workbuf *wbuf;
String *str;
Slice *slice;
Iface *iface;
Eface *eface;
Type *typ;
MSpan *s;
PageID k;
bool keepworking;
// Cache memory arena parameters in local vars.
arena_start = runtime·mheap.arena_start;
arena_used = runtime·mheap.arena_used;
wbuf = getempty(nil);
nobj = wbuf->nobj;
wp = &wbuf->obj[nobj];
keepworking = b == nil;
scanbufpos = 0;
for(i = 0; i < nelem(scanbuf); i++)
scanbuf[i] = nil;
ptrbitp = nil;
cached = 0;
ncached = 0;
// ptrmask can have 3 possible values:
// 1. nil - obtain pointer mask from GC bitmap.
// 2. ScanConservatively - don't use any mask, scan conservatively.
// 3. pointer to a compact mask (for stacks and data).
if(b != nil)
goto scanobj;
for(;;) {
if(nobj == 0) {
// Out of work in workbuf.
// First, see is there is any work in scanbuf.
for(i = 0; i < nelem(scanbuf); i++) {
b = scanbuf[scanbufpos];
scanbuf[scanbufpos++] = nil;
if(scanbufpos == nelem(scanbuf))
scanbufpos = 0;
if(b != nil) {
n = arena_used - b; // scan until bitBoundary or BitsDead
ptrmask = nil; // use GC bitmap for pointer info
goto scanobj;
}
}
if(!keepworking) {
putempty(wbuf);
return;
}
// Refill workbuf from global queue.
wbuf = getfull(wbuf);
if(wbuf == nil)
return;
nobj = wbuf->nobj;
wp = &wbuf->obj[nobj];
}
// If another proc wants a pointer, give it some.
if(work.nwait > 0 && nobj > 4 && work.full == 0) {
wbuf->nobj = nobj;
wbuf = handoff(wbuf);
nobj = wbuf->nobj;
wp = &wbuf->obj[nobj];
}
wp--;
nobj--;
b = *wp;
n = arena_used - b; // scan until next bitBoundary or BitsDead
ptrmask = nil; // use GC bitmap for pointer info
scanobj:
if(!PreciseScan) {
if(ptrmask == nil) {
// Heap obj, obtain real size.
if(!runtime·mlookup(b, &p, &n, nil))
continue; // not an allocated obj
if(b != p)
runtime·throw("bad heap object");
}
ptrmask = ScanConservatively;
}
// Find bits of the beginning of the object.
if(ptrmask == nil) {
off = (uintptr*)b - (uintptr*)arena_start;
ptrbitp = arena_start - off/wordsPerBitmapByte - 1;
shift = (off % wordsPerBitmapByte) * gcBits;
cached = *ptrbitp >> shift;
cached &= ~bitBoundary;
ncached = (8 - shift)/gcBits;
}
for(i = 0; i < n; i += PtrSize) {
obj = nil;
// Find bits for this word.
if(ptrmask == nil) {
// Check is we have reached end of span.
if((((uintptr)b+i)%PageSize) == 0 &&
runtime·mheap.spans[(b-arena_start)>>PageShift] != runtime·mheap.spans[(b+i-arena_start)>>PageShift])
break;
// Consult GC bitmap.
if(ncached <= 0) {
// Refill cache.
cached = *--ptrbitp;
ncached = 2;
}
bits = cached;
cached >>= gcBits;
ncached--;
if((bits&bitBoundary) != 0)
break; // reached beginning of the next object
bits = (bits>>2)&BitsMask;
if(bits == BitsDead)
break; // reached no-scan part of the object
} else if(ptrmask != ScanConservatively) // dense mask (stack or data)
bits = (ptrmask[(i/PtrSize)/4]>>(((i/PtrSize)%4)*BitsPerPointer))&BitsMask;
else
bits = BitsPointer;
if(bits == BitsScalar || bits == BitsDead)
continue;
if(bits == BitsPointer) {
obj = *(byte**)(b+i);
goto markobj;
}
// Find the next pair of bits.
if(ptrmask == nil) {
if(ncached <= 0) {
// Refill cache.
cached = *--ptrbitp;
ncached = 2;
}
bits = (cached>>2)&BitsMask;
} else
bits = (ptrmask[((i+PtrSize)/PtrSize)/4]>>((((i+PtrSize)/PtrSize)%4)*BitsPerPointer))&BitsMask;
switch(bits) {
case BitsString:
str = (String*)(b+i);
if(str->len > 0)
obj = str->str;
break;
case BitsSlice:
slice = (Slice*)(b+i);
if(Debug && slice->cap < slice->len) {
g->m->traceback = 2;
runtime·printf("bad slice in object %p: %p/%p/%p\n",
b, slice->array, slice->len, slice->cap);
runtime·throw("bad slice in heap object");
}
if(slice->cap > 0)
obj = slice->array;
break;
case BitsIface:
iface = (Iface*)(b+i);
if(iface->tab != nil) {
typ = iface->tab->type;
if(!(typ->kind&KindDirectIface) || !(typ->kind&KindNoPointers))
obj = iface->data;
}
break;
case BitsEface:
eface = (Eface*)(b+i);
typ = eface->type;
if(typ != nil) {
if(!(typ->kind&KindDirectIface) || !(typ->kind&KindNoPointers))
obj = eface->data;
}
break;
}
if(bits == BitsSlice) {
i += 2*PtrSize;
if(ncached == 2)
ncached = 0;
else if(ptrmask == nil) {
// Refill cache and consume one quadruple.
cached = *--ptrbitp;
cached >>= gcBits;
ncached = 1;
}
} else {
i += PtrSize;
cached >>= gcBits;
ncached--;
}
markobj:
// At this point we have extracted the next potential pointer.
// Check if it points into heap.
if(obj == nil || obj < arena_start || obj >= arena_used)
continue;
// Mark the object.
off = (uintptr*)obj - (uintptr*)arena_start;
bitp = arena_start - off/wordsPerBitmapByte - 1;
shift = (off % wordsPerBitmapByte) * gcBits;
xbits = *bitp;
bits = (xbits >> shift) & bitMask;
if((bits&bitBoundary) == 0) {
// Not a beginning of a block, consult span table to find the block beginning.
k = (uintptr)obj>>PageShift;
x = k;
x -= (uintptr)arena_start>>PageShift;
s = runtime·mheap.spans[x];
if(s == nil || k < s->start || obj >= s->limit || s->state != MSpanInUse)
continue;
p = (byte*)((uintptr)s->start<<PageShift);
if(s->sizeclass != 0) {
size = s->elemsize;
idx = ((byte*)obj - p)/size;
p = p+idx*size;
}
if(p == obj) {
runtime·printf("runtime: failed to find block beginning for %p s=%p s->limit=%p\n",
p, s->start*PageSize, s->limit);
runtime·throw("failed to find block beginning");
}
obj = p;
goto markobj;
}
// Now we have bits, bitp, and shift correct for
// obj pointing at the base of the object.
// Only care about not marked objects.
if((bits&bitMarked) != 0)
continue;
// If obj size is greater than 8, then each byte of GC bitmap
// contains info for at most one object. In such case we use
// non-atomic byte store to mark the object. This can lead
// to double enqueue of the object for scanning, but scanning
// is an idempotent operation, so it is OK. This cannot lead
// to bitmap corruption because the single marked bit is the
// only thing that can change in the byte.
// For 8-byte objects we use non-atomic store, if the other
// quadruple is already marked. Otherwise we resort to CAS
// loop for marking.
if((xbits&(bitMask|(bitMask<<gcBits))) != (bitBoundary|(bitBoundary<<gcBits)) ||
work.nproc == 1)
*bitp = xbits | (bitMarked<<shift);
else
runtime·atomicor8(bitp, bitMarked<<shift);
if(((xbits>>(shift+2))&BitsMask) == BitsDead)
continue; // noscan object
// Queue the obj for scanning.
PREFETCH(obj);
obj = (byte*)((uintptr)obj & ~(PtrSize-1));
p = scanbuf[scanbufpos];
scanbuf[scanbufpos++] = obj;
if(scanbufpos == nelem(scanbuf))
scanbufpos = 0;
if(p == nil)
continue;
// If workbuf is full, obtain an empty one.
if(nobj >= nelem(wbuf->obj)) {
wbuf->nobj = nobj;
wbuf = getempty(wbuf);
nobj = wbuf->nobj;
wp = &wbuf->obj[nobj];
}
*wp = p;
wp++;
nobj++;
}
if(Debug && ptrmask == nil) {
// For heap objects ensure that we did not overscan.
n = 0;
p = nil;
if(!runtime·mlookup(b, &p, &n, nil) || b != p || i > n) {
runtime·printf("runtime: scanned (%p,%p), heap object (%p,%p)\n", b, i, p, n);
runtime·throw("scanblock: scanned invalid object");
}
}
}
}
runtime: faster GC mark phase Also bump MaxGcproc to 8. benchmark old ns/op new ns/op delta Parser 3796323000 3763880000 -0.85% Parser-2 3591752500 3518560250 -2.04% Parser-4 3423825250 3334955250 -2.60% Parser-8 3304585500 3267014750 -1.14% Parser-16 3313615750 3286160500 -0.83% Tree 984128500 942501166 -4.23% Tree-2 932564444 883266222 -5.29% Tree-4 835831000 799912777 -4.30% Tree-8 819238500 789717333 -3.73% Tree-16 880837833 837840055 -5.13% Tree2 604698100 579716900 -4.13% Tree2-2 372414500 356765200 -4.20% Tree2-4 187488100 177455900 -5.56% Tree2-8 136315300 102086700 -25.11% Tree2-16 93725900 76705800 -22.18% ParserPause 157441210 166202783 +5.56% ParserPause-2 93842650 85199900 -9.21% ParserPause-4 56844404 53535684 -5.82% ParserPause-8 35739446 30767613 -16.15% ParserPause-16 32718255 27212441 -16.83% TreePause 29610557 29787725 +0.60% TreePause-2 24001659 20674421 -13.86% TreePause-4 15114887 12842781 -15.03% TreePause-8 13128725 10741747 -22.22% TreePause-16 16131360 12506901 -22.47% Tree2Pause 2673350920 2651045280 -0.83% Tree2Pause-2 1796999200 1709350040 -4.88% Tree2Pause-4 1163553320 1090706480 -6.67% Tree2Pause-8 987032520 858916360 -25.11% Tree2Pause-16 864758560 809567480 -6.81% ParserLastPause 280537000 289047000 +3.03% ParserLastPause-2 183030000 166748000 -8.90% ParserLastPause-4 105817000 91552000 -13.48% ParserLastPause-8 65127000 53288000 -18.18% ParserLastPause-16 45258000 38334000 -15.30% TreeLastPause 45072000 51449000 +12.39% TreeLastPause-2 39269000 37866000 -3.57% TreeLastPause-4 23564000 20649000 -12.37% TreeLastPause-8 20881000 15807000 -24.30% TreeLastPause-16 23297000 17309000 -25.70% Tree2LastPause 6046912000 5797120000 -4.13% Tree2LastPause-2 3724034000 3567592000 -4.20% Tree2LastPause-4 1874831000 1774524000 -5.65% Tree2LastPause-8 1363108000 1020809000 -12.79% Tree2LastPause-16 937208000 767019000 -22.18% R=rsc, 0xe2.0x9a.0x9b CC=golang-dev https://golang.org/cl/6223050
2012-05-24 00:55:50 -06:00
static void
markroot(ParFor *desc, uint32 i)
{
FinBlock *fb;
runtime: concurrent GC sweep Moves sweep phase out of stoptheworld by adding background sweeper goroutine and lazy on-demand sweeping. It turned out to be somewhat trickier than I expected, because there is no point in time when we know size of live heap nor consistent number of mallocs and frees. So everything related to next_gc, mprof, memstats, etc becomes trickier. At the end of GC next_gc is conservatively set to heap_alloc*GOGC, which is much larger than real value. But after every sweep next_gc is decremented by freed*GOGC. So when everything is swept next_gc becomes what it should be. For mprof I had to introduce 3-generation scheme (allocs, revent_allocs, prev_allocs), because by the end of GC we know number of frees for the *previous* GC. Significant caution is required to not cross yet-unknown real value of next_gc. This is achieved by 2 means: 1. Whenever I allocate a span from MCentral, I sweep a span in that MCentral. 2. Whenever I allocate N pages from MHeap, I sweep until at least N pages are returned to heap. This provides quite strong guarantees that heap does not grow when it should now. http-1 allocated 7036 7033 -0.04% allocs 60 60 +0.00% cputime 51050 46700 -8.52% gc-pause-one 34060569 1777993 -94.78% gc-pause-total 2554 133 -94.79% latency-50 178448 170926 -4.22% latency-95 284350 198294 -30.26% latency-99 345191 220652 -36.08% rss 101564416 101007360 -0.55% sys-gc 6606832 6541296 -0.99% sys-heap 88801280 87752704 -1.18% sys-other 7334208 7405928 +0.98% sys-stack 524288 524288 +0.00% sys-total 103266608 102224216 -1.01% time 50339 46533 -7.56% virtual-mem 292990976 293728256 +0.25% garbage-1 allocated 2983818 2990889 +0.24% allocs 62880 62902 +0.03% cputime 16480000 16190000 -1.76% gc-pause-one 828462467 487875135 -41.11% gc-pause-total 4142312 2439375 -41.11% rss 1151709184 1153712128 +0.17% sys-gc 66068352 66068352 +0.00% sys-heap 1039728640 1039728640 +0.00% sys-other 37776064 40770176 +7.93% sys-stack 8781824 8781824 +0.00% sys-total 1152354880 1155348992 +0.26% time 16496998 16199876 -1.80% virtual-mem 1409564672 1402281984 -0.52% LGTM=rsc R=golang-codereviews, sameer, rsc, iant, jeremyjackins, gobot CC=golang-codereviews, khr https://golang.org/cl/46430043
2014-02-12 11:16:42 -07:00
MHeap *h;
MSpan **allspans, *s;
runtime: concurrent GC sweep Moves sweep phase out of stoptheworld by adding background sweeper goroutine and lazy on-demand sweeping. It turned out to be somewhat trickier than I expected, because there is no point in time when we know size of live heap nor consistent number of mallocs and frees. So everything related to next_gc, mprof, memstats, etc becomes trickier. At the end of GC next_gc is conservatively set to heap_alloc*GOGC, which is much larger than real value. But after every sweep next_gc is decremented by freed*GOGC. So when everything is swept next_gc becomes what it should be. For mprof I had to introduce 3-generation scheme (allocs, revent_allocs, prev_allocs), because by the end of GC we know number of frees for the *previous* GC. Significant caution is required to not cross yet-unknown real value of next_gc. This is achieved by 2 means: 1. Whenever I allocate a span from MCentral, I sweep a span in that MCentral. 2. Whenever I allocate N pages from MHeap, I sweep until at least N pages are returned to heap. This provides quite strong guarantees that heap does not grow when it should now. http-1 allocated 7036 7033 -0.04% allocs 60 60 +0.00% cputime 51050 46700 -8.52% gc-pause-one 34060569 1777993 -94.78% gc-pause-total 2554 133 -94.79% latency-50 178448 170926 -4.22% latency-95 284350 198294 -30.26% latency-99 345191 220652 -36.08% rss 101564416 101007360 -0.55% sys-gc 6606832 6541296 -0.99% sys-heap 88801280 87752704 -1.18% sys-other 7334208 7405928 +0.98% sys-stack 524288 524288 +0.00% sys-total 103266608 102224216 -1.01% time 50339 46533 -7.56% virtual-mem 292990976 293728256 +0.25% garbage-1 allocated 2983818 2990889 +0.24% allocs 62880 62902 +0.03% cputime 16480000 16190000 -1.76% gc-pause-one 828462467 487875135 -41.11% gc-pause-total 4142312 2439375 -41.11% rss 1151709184 1153712128 +0.17% sys-gc 66068352 66068352 +0.00% sys-heap 1039728640 1039728640 +0.00% sys-other 37776064 40770176 +7.93% sys-stack 8781824 8781824 +0.00% sys-total 1152354880 1155348992 +0.26% time 16496998 16199876 -1.80% virtual-mem 1409564672 1402281984 -0.52% LGTM=rsc R=golang-codereviews, sameer, rsc, iant, jeremyjackins, gobot CC=golang-codereviews, khr https://golang.org/cl/46430043
2014-02-12 11:16:42 -07:00
uint32 spanidx, sg;
G *gp;
void *p;
runtime: faster GC mark phase Also bump MaxGcproc to 8. benchmark old ns/op new ns/op delta Parser 3796323000 3763880000 -0.85% Parser-2 3591752500 3518560250 -2.04% Parser-4 3423825250 3334955250 -2.60% Parser-8 3304585500 3267014750 -1.14% Parser-16 3313615750 3286160500 -0.83% Tree 984128500 942501166 -4.23% Tree-2 932564444 883266222 -5.29% Tree-4 835831000 799912777 -4.30% Tree-8 819238500 789717333 -3.73% Tree-16 880837833 837840055 -5.13% Tree2 604698100 579716900 -4.13% Tree2-2 372414500 356765200 -4.20% Tree2-4 187488100 177455900 -5.56% Tree2-8 136315300 102086700 -25.11% Tree2-16 93725900 76705800 -22.18% ParserPause 157441210 166202783 +5.56% ParserPause-2 93842650 85199900 -9.21% ParserPause-4 56844404 53535684 -5.82% ParserPause-8 35739446 30767613 -16.15% ParserPause-16 32718255 27212441 -16.83% TreePause 29610557 29787725 +0.60% TreePause-2 24001659 20674421 -13.86% TreePause-4 15114887 12842781 -15.03% TreePause-8 13128725 10741747 -22.22% TreePause-16 16131360 12506901 -22.47% Tree2Pause 2673350920 2651045280 -0.83% Tree2Pause-2 1796999200 1709350040 -4.88% Tree2Pause-4 1163553320 1090706480 -6.67% Tree2Pause-8 987032520 858916360 -25.11% Tree2Pause-16 864758560 809567480 -6.81% ParserLastPause 280537000 289047000 +3.03% ParserLastPause-2 183030000 166748000 -8.90% ParserLastPause-4 105817000 91552000 -13.48% ParserLastPause-8 65127000 53288000 -18.18% ParserLastPause-16 45258000 38334000 -15.30% TreeLastPause 45072000 51449000 +12.39% TreeLastPause-2 39269000 37866000 -3.57% TreeLastPause-4 23564000 20649000 -12.37% TreeLastPause-8 20881000 15807000 -24.30% TreeLastPause-16 23297000 17309000 -25.70% Tree2LastPause 6046912000 5797120000 -4.13% Tree2LastPause-2 3724034000 3567592000 -4.20% Tree2LastPause-4 1874831000 1774524000 -5.65% Tree2LastPause-8 1363108000 1020809000 -12.79% Tree2LastPause-16 937208000 767019000 -22.18% R=rsc, 0xe2.0x9a.0x9b CC=golang-dev https://golang.org/cl/6223050
2012-05-24 00:55:50 -06:00
USED(&desc);
// Note: if you add a case here, please also update heapdump.c:dumproots.
switch(i) {
case RootData:
scanblock(data, edata - data, runtime·gcdatamask);
break;
case RootBss:
scanblock(bss, ebss - bss, runtime·gcbssmask);
break;
case RootFinalizers:
for(fb=allfin; fb; fb=fb->alllink)
scanblock((byte*)fb->fin, fb->cnt*sizeof(fb->fin[0]), ScanConservatively);
break;
case RootSpans:
// mark MSpan.specials
runtime: concurrent GC sweep Moves sweep phase out of stoptheworld by adding background sweeper goroutine and lazy on-demand sweeping. It turned out to be somewhat trickier than I expected, because there is no point in time when we know size of live heap nor consistent number of mallocs and frees. So everything related to next_gc, mprof, memstats, etc becomes trickier. At the end of GC next_gc is conservatively set to heap_alloc*GOGC, which is much larger than real value. But after every sweep next_gc is decremented by freed*GOGC. So when everything is swept next_gc becomes what it should be. For mprof I had to introduce 3-generation scheme (allocs, revent_allocs, prev_allocs), because by the end of GC we know number of frees for the *previous* GC. Significant caution is required to not cross yet-unknown real value of next_gc. This is achieved by 2 means: 1. Whenever I allocate a span from MCentral, I sweep a span in that MCentral. 2. Whenever I allocate N pages from MHeap, I sweep until at least N pages are returned to heap. This provides quite strong guarantees that heap does not grow when it should now. http-1 allocated 7036 7033 -0.04% allocs 60 60 +0.00% cputime 51050 46700 -8.52% gc-pause-one 34060569 1777993 -94.78% gc-pause-total 2554 133 -94.79% latency-50 178448 170926 -4.22% latency-95 284350 198294 -30.26% latency-99 345191 220652 -36.08% rss 101564416 101007360 -0.55% sys-gc 6606832 6541296 -0.99% sys-heap 88801280 87752704 -1.18% sys-other 7334208 7405928 +0.98% sys-stack 524288 524288 +0.00% sys-total 103266608 102224216 -1.01% time 50339 46533 -7.56% virtual-mem 292990976 293728256 +0.25% garbage-1 allocated 2983818 2990889 +0.24% allocs 62880 62902 +0.03% cputime 16480000 16190000 -1.76% gc-pause-one 828462467 487875135 -41.11% gc-pause-total 4142312 2439375 -41.11% rss 1151709184 1153712128 +0.17% sys-gc 66068352 66068352 +0.00% sys-heap 1039728640 1039728640 +0.00% sys-other 37776064 40770176 +7.93% sys-stack 8781824 8781824 +0.00% sys-total 1152354880 1155348992 +0.26% time 16496998 16199876 -1.80% virtual-mem 1409564672 1402281984 -0.52% LGTM=rsc R=golang-codereviews, sameer, rsc, iant, jeremyjackins, gobot CC=golang-codereviews, khr https://golang.org/cl/46430043
2014-02-12 11:16:42 -07:00
h = &runtime·mheap;
sg = h->sweepgen;
allspans = h->allspans;
for(spanidx=0; spanidx<runtime·mheap.nspan; spanidx++) {
Special *sp;
SpecialFinalizer *spf;
s = allspans[spanidx];
if(s->state != MSpanInUse)
continue;
if(s->sweepgen != sg) {
runtime·printf("sweep %d %d\n", s->sweepgen, sg);
runtime: concurrent GC sweep Moves sweep phase out of stoptheworld by adding background sweeper goroutine and lazy on-demand sweeping. It turned out to be somewhat trickier than I expected, because there is no point in time when we know size of live heap nor consistent number of mallocs and frees. So everything related to next_gc, mprof, memstats, etc becomes trickier. At the end of GC next_gc is conservatively set to heap_alloc*GOGC, which is much larger than real value. But after every sweep next_gc is decremented by freed*GOGC. So when everything is swept next_gc becomes what it should be. For mprof I had to introduce 3-generation scheme (allocs, revent_allocs, prev_allocs), because by the end of GC we know number of frees for the *previous* GC. Significant caution is required to not cross yet-unknown real value of next_gc. This is achieved by 2 means: 1. Whenever I allocate a span from MCentral, I sweep a span in that MCentral. 2. Whenever I allocate N pages from MHeap, I sweep until at least N pages are returned to heap. This provides quite strong guarantees that heap does not grow when it should now. http-1 allocated 7036 7033 -0.04% allocs 60 60 +0.00% cputime 51050 46700 -8.52% gc-pause-one 34060569 1777993 -94.78% gc-pause-total 2554 133 -94.79% latency-50 178448 170926 -4.22% latency-95 284350 198294 -30.26% latency-99 345191 220652 -36.08% rss 101564416 101007360 -0.55% sys-gc 6606832 6541296 -0.99% sys-heap 88801280 87752704 -1.18% sys-other 7334208 7405928 +0.98% sys-stack 524288 524288 +0.00% sys-total 103266608 102224216 -1.01% time 50339 46533 -7.56% virtual-mem 292990976 293728256 +0.25% garbage-1 allocated 2983818 2990889 +0.24% allocs 62880 62902 +0.03% cputime 16480000 16190000 -1.76% gc-pause-one 828462467 487875135 -41.11% gc-pause-total 4142312 2439375 -41.11% rss 1151709184 1153712128 +0.17% sys-gc 66068352 66068352 +0.00% sys-heap 1039728640 1039728640 +0.00% sys-other 37776064 40770176 +7.93% sys-stack 8781824 8781824 +0.00% sys-total 1152354880 1155348992 +0.26% time 16496998 16199876 -1.80% virtual-mem 1409564672 1402281984 -0.52% LGTM=rsc R=golang-codereviews, sameer, rsc, iant, jeremyjackins, gobot CC=golang-codereviews, khr https://golang.org/cl/46430043
2014-02-12 11:16:42 -07:00
runtime·throw("gc: unswept span");
}
for(sp = s->specials; sp != nil; sp = sp->next) {
if(sp->kind != KindSpecialFinalizer)
continue;
// don't mark finalized object, but scan it so we
// retain everything it points to.
spf = (SpecialFinalizer*)sp;
// A finalizer can be set for an inner byte of an object, find object beginning.
p = (void*)((s->start << PageShift) + spf->special.offset/s->elemsize*s->elemsize);
scanblock(p, s->elemsize, nil);
scanblock((void*)&spf->fn, PtrSize, ScanConservatively);
}
}
break;
case RootFlushCaches:
flushallmcaches();
break;
default:
// the rest is scanning goroutine stacks
if(i - RootCount >= runtime·allglen)
runtime·throw("markroot: bad index");
gp = runtime·allg[i - RootCount];
// remember when we've first observed the G blocked
// needed only to output in traceback
if((gp->status == Gwaiting || gp->status == Gsyscall) && gp->waitsince == 0)
gp->waitsince = work.tstart;
// Shrink a stack if not much of it is being used.
runtime·shrinkstack(gp);
scanstack(gp);
break;
}
runtime: faster GC mark phase Also bump MaxGcproc to 8. benchmark old ns/op new ns/op delta Parser 3796323000 3763880000 -0.85% Parser-2 3591752500 3518560250 -2.04% Parser-4 3423825250 3334955250 -2.60% Parser-8 3304585500 3267014750 -1.14% Parser-16 3313615750 3286160500 -0.83% Tree 984128500 942501166 -4.23% Tree-2 932564444 883266222 -5.29% Tree-4 835831000 799912777 -4.30% Tree-8 819238500 789717333 -3.73% Tree-16 880837833 837840055 -5.13% Tree2 604698100 579716900 -4.13% Tree2-2 372414500 356765200 -4.20% Tree2-4 187488100 177455900 -5.56% Tree2-8 136315300 102086700 -25.11% Tree2-16 93725900 76705800 -22.18% ParserPause 157441210 166202783 +5.56% ParserPause-2 93842650 85199900 -9.21% ParserPause-4 56844404 53535684 -5.82% ParserPause-8 35739446 30767613 -16.15% ParserPause-16 32718255 27212441 -16.83% TreePause 29610557 29787725 +0.60% TreePause-2 24001659 20674421 -13.86% TreePause-4 15114887 12842781 -15.03% TreePause-8 13128725 10741747 -22.22% TreePause-16 16131360 12506901 -22.47% Tree2Pause 2673350920 2651045280 -0.83% Tree2Pause-2 1796999200 1709350040 -4.88% Tree2Pause-4 1163553320 1090706480 -6.67% Tree2Pause-8 987032520 858916360 -25.11% Tree2Pause-16 864758560 809567480 -6.81% ParserLastPause 280537000 289047000 +3.03% ParserLastPause-2 183030000 166748000 -8.90% ParserLastPause-4 105817000 91552000 -13.48% ParserLastPause-8 65127000 53288000 -18.18% ParserLastPause-16 45258000 38334000 -15.30% TreeLastPause 45072000 51449000 +12.39% TreeLastPause-2 39269000 37866000 -3.57% TreeLastPause-4 23564000 20649000 -12.37% TreeLastPause-8 20881000 15807000 -24.30% TreeLastPause-16 23297000 17309000 -25.70% Tree2LastPause 6046912000 5797120000 -4.13% Tree2LastPause-2 3724034000 3567592000 -4.20% Tree2LastPause-4 1874831000 1774524000 -5.65% Tree2LastPause-8 1363108000 1020809000 -12.79% Tree2LastPause-16 937208000 767019000 -22.18% R=rsc, 0xe2.0x9a.0x9b CC=golang-dev https://golang.org/cl/6223050
2012-05-24 00:55:50 -06:00
}
// Get an empty work buffer off the work.empty list,
// allocating new buffers as needed.
static Workbuf*
getempty(Workbuf *b)
{
MCache *c;
runtime: faster GC mark phase Also bump MaxGcproc to 8. benchmark old ns/op new ns/op delta Parser 3796323000 3763880000 -0.85% Parser-2 3591752500 3518560250 -2.04% Parser-4 3423825250 3334955250 -2.60% Parser-8 3304585500 3267014750 -1.14% Parser-16 3313615750 3286160500 -0.83% Tree 984128500 942501166 -4.23% Tree-2 932564444 883266222 -5.29% Tree-4 835831000 799912777 -4.30% Tree-8 819238500 789717333 -3.73% Tree-16 880837833 837840055 -5.13% Tree2 604698100 579716900 -4.13% Tree2-2 372414500 356765200 -4.20% Tree2-4 187488100 177455900 -5.56% Tree2-8 136315300 102086700 -25.11% Tree2-16 93725900 76705800 -22.18% ParserPause 157441210 166202783 +5.56% ParserPause-2 93842650 85199900 -9.21% ParserPause-4 56844404 53535684 -5.82% ParserPause-8 35739446 30767613 -16.15% ParserPause-16 32718255 27212441 -16.83% TreePause 29610557 29787725 +0.60% TreePause-2 24001659 20674421 -13.86% TreePause-4 15114887 12842781 -15.03% TreePause-8 13128725 10741747 -22.22% TreePause-16 16131360 12506901 -22.47% Tree2Pause 2673350920 2651045280 -0.83% Tree2Pause-2 1796999200 1709350040 -4.88% Tree2Pause-4 1163553320 1090706480 -6.67% Tree2Pause-8 987032520 858916360 -25.11% Tree2Pause-16 864758560 809567480 -6.81% ParserLastPause 280537000 289047000 +3.03% ParserLastPause-2 183030000 166748000 -8.90% ParserLastPause-4 105817000 91552000 -13.48% ParserLastPause-8 65127000 53288000 -18.18% ParserLastPause-16 45258000 38334000 -15.30% TreeLastPause 45072000 51449000 +12.39% TreeLastPause-2 39269000 37866000 -3.57% TreeLastPause-4 23564000 20649000 -12.37% TreeLastPause-8 20881000 15807000 -24.30% TreeLastPause-16 23297000 17309000 -25.70% Tree2LastPause 6046912000 5797120000 -4.13% Tree2LastPause-2 3724034000 3567592000 -4.20% Tree2LastPause-4 1874831000 1774524000 -5.65% Tree2LastPause-8 1363108000 1020809000 -12.79% Tree2LastPause-16 937208000 767019000 -22.18% R=rsc, 0xe2.0x9a.0x9b CC=golang-dev https://golang.org/cl/6223050
2012-05-24 00:55:50 -06:00
if(b != nil)
runtime·lfstackpush(&work.full, &b->node);
b = nil;
c = g->m->mcache;
if(c->gcworkbuf != nil) {
b = c->gcworkbuf;
c->gcworkbuf = nil;
}
if(b == nil)
b = (Workbuf*)runtime·lfstackpop(&work.empty);
if(b == nil)
b = runtime·persistentalloc(sizeof(*b), CacheLineSize, &mstats.gc_sys);
b->nobj = 0;
return b;
}
static void
putempty(Workbuf *b)
{
MCache *c;
c = g->m->mcache;
if(c->gcworkbuf == nil) {
c->gcworkbuf = b;
return;
}
runtime: faster GC mark phase Also bump MaxGcproc to 8. benchmark old ns/op new ns/op delta Parser 3796323000 3763880000 -0.85% Parser-2 3591752500 3518560250 -2.04% Parser-4 3423825250 3334955250 -2.60% Parser-8 3304585500 3267014750 -1.14% Parser-16 3313615750 3286160500 -0.83% Tree 984128500 942501166 -4.23% Tree-2 932564444 883266222 -5.29% Tree-4 835831000 799912777 -4.30% Tree-8 819238500 789717333 -3.73% Tree-16 880837833 837840055 -5.13% Tree2 604698100 579716900 -4.13% Tree2-2 372414500 356765200 -4.20% Tree2-4 187488100 177455900 -5.56% Tree2-8 136315300 102086700 -25.11% Tree2-16 93725900 76705800 -22.18% ParserPause 157441210 166202783 +5.56% ParserPause-2 93842650 85199900 -9.21% ParserPause-4 56844404 53535684 -5.82% ParserPause-8 35739446 30767613 -16.15% ParserPause-16 32718255 27212441 -16.83% TreePause 29610557 29787725 +0.60% TreePause-2 24001659 20674421 -13.86% TreePause-4 15114887 12842781 -15.03% TreePause-8 13128725 10741747 -22.22% TreePause-16 16131360 12506901 -22.47% Tree2Pause 2673350920 2651045280 -0.83% Tree2Pause-2 1796999200 1709350040 -4.88% Tree2Pause-4 1163553320 1090706480 -6.67% Tree2Pause-8 987032520 858916360 -25.11% Tree2Pause-16 864758560 809567480 -6.81% ParserLastPause 280537000 289047000 +3.03% ParserLastPause-2 183030000 166748000 -8.90% ParserLastPause-4 105817000 91552000 -13.48% ParserLastPause-8 65127000 53288000 -18.18% ParserLastPause-16 45258000 38334000 -15.30% TreeLastPause 45072000 51449000 +12.39% TreeLastPause-2 39269000 37866000 -3.57% TreeLastPause-4 23564000 20649000 -12.37% TreeLastPause-8 20881000 15807000 -24.30% TreeLastPause-16 23297000 17309000 -25.70% Tree2LastPause 6046912000 5797120000 -4.13% Tree2LastPause-2 3724034000 3567592000 -4.20% Tree2LastPause-4 1874831000 1774524000 -5.65% Tree2LastPause-8 1363108000 1020809000 -12.79% Tree2LastPause-16 937208000 767019000 -22.18% R=rsc, 0xe2.0x9a.0x9b CC=golang-dev https://golang.org/cl/6223050
2012-05-24 00:55:50 -06:00
runtime·lfstackpush(&work.empty, &b->node);
}
void
runtime·gcworkbuffree(void *b)
{
if(b != nil)
putempty(b);
}
// Get a full work buffer off the work.full list, or return nil.
static Workbuf*
getfull(Workbuf *b)
{
int32 i;
runtime: faster GC mark phase Also bump MaxGcproc to 8. benchmark old ns/op new ns/op delta Parser 3796323000 3763880000 -0.85% Parser-2 3591752500 3518560250 -2.04% Parser-4 3423825250 3334955250 -2.60% Parser-8 3304585500 3267014750 -1.14% Parser-16 3313615750 3286160500 -0.83% Tree 984128500 942501166 -4.23% Tree-2 932564444 883266222 -5.29% Tree-4 835831000 799912777 -4.30% Tree-8 819238500 789717333 -3.73% Tree-16 880837833 837840055 -5.13% Tree2 604698100 579716900 -4.13% Tree2-2 372414500 356765200 -4.20% Tree2-4 187488100 177455900 -5.56% Tree2-8 136315300 102086700 -25.11% Tree2-16 93725900 76705800 -22.18% ParserPause 157441210 166202783 +5.56% ParserPause-2 93842650 85199900 -9.21% ParserPause-4 56844404 53535684 -5.82% ParserPause-8 35739446 30767613 -16.15% ParserPause-16 32718255 27212441 -16.83% TreePause 29610557 29787725 +0.60% TreePause-2 24001659 20674421 -13.86% TreePause-4 15114887 12842781 -15.03% TreePause-8 13128725 10741747 -22.22% TreePause-16 16131360 12506901 -22.47% Tree2Pause 2673350920 2651045280 -0.83% Tree2Pause-2 1796999200 1709350040 -4.88% Tree2Pause-4 1163553320 1090706480 -6.67% Tree2Pause-8 987032520 858916360 -25.11% Tree2Pause-16 864758560 809567480 -6.81% ParserLastPause 280537000 289047000 +3.03% ParserLastPause-2 183030000 166748000 -8.90% ParserLastPause-4 105817000 91552000 -13.48% ParserLastPause-8 65127000 53288000 -18.18% ParserLastPause-16 45258000 38334000 -15.30% TreeLastPause 45072000 51449000 +12.39% TreeLastPause-2 39269000 37866000 -3.57% TreeLastPause-4 23564000 20649000 -12.37% TreeLastPause-8 20881000 15807000 -24.30% TreeLastPause-16 23297000 17309000 -25.70% Tree2LastPause 6046912000 5797120000 -4.13% Tree2LastPause-2 3724034000 3567592000 -4.20% Tree2LastPause-4 1874831000 1774524000 -5.65% Tree2LastPause-8 1363108000 1020809000 -12.79% Tree2LastPause-16 937208000 767019000 -22.18% R=rsc, 0xe2.0x9a.0x9b CC=golang-dev https://golang.org/cl/6223050
2012-05-24 00:55:50 -06:00
if(b != nil)
runtime·lfstackpush(&work.empty, &b->node);
b = (Workbuf*)runtime·lfstackpop(&work.full);
if(b != nil || work.nproc == 1)
return b;
runtime·xadd(&work.nwait, +1);
for(i=0;; i++) {
runtime: faster GC mark phase Also bump MaxGcproc to 8. benchmark old ns/op new ns/op delta Parser 3796323000 3763880000 -0.85% Parser-2 3591752500 3518560250 -2.04% Parser-4 3423825250 3334955250 -2.60% Parser-8 3304585500 3267014750 -1.14% Parser-16 3313615750 3286160500 -0.83% Tree 984128500 942501166 -4.23% Tree-2 932564444 883266222 -5.29% Tree-4 835831000 799912777 -4.30% Tree-8 819238500 789717333 -3.73% Tree-16 880837833 837840055 -5.13% Tree2 604698100 579716900 -4.13% Tree2-2 372414500 356765200 -4.20% Tree2-4 187488100 177455900 -5.56% Tree2-8 136315300 102086700 -25.11% Tree2-16 93725900 76705800 -22.18% ParserPause 157441210 166202783 +5.56% ParserPause-2 93842650 85199900 -9.21% ParserPause-4 56844404 53535684 -5.82% ParserPause-8 35739446 30767613 -16.15% ParserPause-16 32718255 27212441 -16.83% TreePause 29610557 29787725 +0.60% TreePause-2 24001659 20674421 -13.86% TreePause-4 15114887 12842781 -15.03% TreePause-8 13128725 10741747 -22.22% TreePause-16 16131360 12506901 -22.47% Tree2Pause 2673350920 2651045280 -0.83% Tree2Pause-2 1796999200 1709350040 -4.88% Tree2Pause-4 1163553320 1090706480 -6.67% Tree2Pause-8 987032520 858916360 -25.11% Tree2Pause-16 864758560 809567480 -6.81% ParserLastPause 280537000 289047000 +3.03% ParserLastPause-2 183030000 166748000 -8.90% ParserLastPause-4 105817000 91552000 -13.48% ParserLastPause-8 65127000 53288000 -18.18% ParserLastPause-16 45258000 38334000 -15.30% TreeLastPause 45072000 51449000 +12.39% TreeLastPause-2 39269000 37866000 -3.57% TreeLastPause-4 23564000 20649000 -12.37% TreeLastPause-8 20881000 15807000 -24.30% TreeLastPause-16 23297000 17309000 -25.70% Tree2LastPause 6046912000 5797120000 -4.13% Tree2LastPause-2 3724034000 3567592000 -4.20% Tree2LastPause-4 1874831000 1774524000 -5.65% Tree2LastPause-8 1363108000 1020809000 -12.79% Tree2LastPause-16 937208000 767019000 -22.18% R=rsc, 0xe2.0x9a.0x9b CC=golang-dev https://golang.org/cl/6223050
2012-05-24 00:55:50 -06:00
if(work.full != 0) {
runtime·xadd(&work.nwait, -1);
b = (Workbuf*)runtime·lfstackpop(&work.full);
if(b != nil)
return b;
runtime·xadd(&work.nwait, +1);
}
if(work.nwait == work.nproc)
return nil;
runtime: make GC stats per-M This is factored out part of: https://golang.org/cl/5279048/ (Parallel GC) benchmark old ns/op new ns/op delta garbage.BenchmarkParser 3999106750 3975026500 -0.60% garbage.BenchmarkParser-2 3720553750 3719196500 -0.04% garbage.BenchmarkParser-4 3502857000 3474980500 -0.80% garbage.BenchmarkParser-8 3375448000 3341310500 -1.01% garbage.BenchmarkParserLastPause 329401000 324097000 -1.61% garbage.BenchmarkParserLastPause-2 208953000 214222000 +2.52% garbage.BenchmarkParserLastPause-4 110933000 111656000 +0.65% garbage.BenchmarkParserLastPause-8 71969000 78230000 +8.70% garbage.BenchmarkParserPause 230808842 197237400 -14.55% garbage.BenchmarkParserPause-2 123674365 125197595 +1.23% garbage.BenchmarkParserPause-4 80518525 85710333 +6.45% garbage.BenchmarkParserPause-8 58310243 56940512 -2.35% garbage.BenchmarkTree2 31471700 31289400 -0.58% garbage.BenchmarkTree2-2 21536800 21086300 -2.09% garbage.BenchmarkTree2-4 11074700 10880000 -1.76% garbage.BenchmarkTree2-8 7568600 7351400 -2.87% garbage.BenchmarkTree2LastPause 314664000 312840000 -0.58% garbage.BenchmarkTree2LastPause-2 215319000 210815000 -2.09% garbage.BenchmarkTree2LastPause-4 110698000 108751000 -1.76% garbage.BenchmarkTree2LastPause-8 75635000 73463000 -2.87% garbage.BenchmarkTree2Pause 174280857 173147571 -0.65% garbage.BenchmarkTree2Pause-2 131332714 129665761 -1.27% garbage.BenchmarkTree2Pause-4 93803095 93422904 -0.41% garbage.BenchmarkTree2Pause-8 86242333 85146761 -1.27% R=rsc CC=golang-dev https://golang.org/cl/5987045
2012-04-05 10:48:28 -06:00
if(i < 10) {
2014-06-26 09:54:39 -06:00
g->m->gcstats.nprocyield++;
runtime·procyield(20);
runtime: make GC stats per-M This is factored out part of: https://golang.org/cl/5279048/ (Parallel GC) benchmark old ns/op new ns/op delta garbage.BenchmarkParser 3999106750 3975026500 -0.60% garbage.BenchmarkParser-2 3720553750 3719196500 -0.04% garbage.BenchmarkParser-4 3502857000 3474980500 -0.80% garbage.BenchmarkParser-8 3375448000 3341310500 -1.01% garbage.BenchmarkParserLastPause 329401000 324097000 -1.61% garbage.BenchmarkParserLastPause-2 208953000 214222000 +2.52% garbage.BenchmarkParserLastPause-4 110933000 111656000 +0.65% garbage.BenchmarkParserLastPause-8 71969000 78230000 +8.70% garbage.BenchmarkParserPause 230808842 197237400 -14.55% garbage.BenchmarkParserPause-2 123674365 125197595 +1.23% garbage.BenchmarkParserPause-4 80518525 85710333 +6.45% garbage.BenchmarkParserPause-8 58310243 56940512 -2.35% garbage.BenchmarkTree2 31471700 31289400 -0.58% garbage.BenchmarkTree2-2 21536800 21086300 -2.09% garbage.BenchmarkTree2-4 11074700 10880000 -1.76% garbage.BenchmarkTree2-8 7568600 7351400 -2.87% garbage.BenchmarkTree2LastPause 314664000 312840000 -0.58% garbage.BenchmarkTree2LastPause-2 215319000 210815000 -2.09% garbage.BenchmarkTree2LastPause-4 110698000 108751000 -1.76% garbage.BenchmarkTree2LastPause-8 75635000 73463000 -2.87% garbage.BenchmarkTree2Pause 174280857 173147571 -0.65% garbage.BenchmarkTree2Pause-2 131332714 129665761 -1.27% garbage.BenchmarkTree2Pause-4 93803095 93422904 -0.41% garbage.BenchmarkTree2Pause-8 86242333 85146761 -1.27% R=rsc CC=golang-dev https://golang.org/cl/5987045
2012-04-05 10:48:28 -06:00
} else if(i < 20) {
2014-06-26 09:54:39 -06:00
g->m->gcstats.nosyield++;
runtime·osyield();
runtime: make GC stats per-M This is factored out part of: https://golang.org/cl/5279048/ (Parallel GC) benchmark old ns/op new ns/op delta garbage.BenchmarkParser 3999106750 3975026500 -0.60% garbage.BenchmarkParser-2 3720553750 3719196500 -0.04% garbage.BenchmarkParser-4 3502857000 3474980500 -0.80% garbage.BenchmarkParser-8 3375448000 3341310500 -1.01% garbage.BenchmarkParserLastPause 329401000 324097000 -1.61% garbage.BenchmarkParserLastPause-2 208953000 214222000 +2.52% garbage.BenchmarkParserLastPause-4 110933000 111656000 +0.65% garbage.BenchmarkParserLastPause-8 71969000 78230000 +8.70% garbage.BenchmarkParserPause 230808842 197237400 -14.55% garbage.BenchmarkParserPause-2 123674365 125197595 +1.23% garbage.BenchmarkParserPause-4 80518525 85710333 +6.45% garbage.BenchmarkParserPause-8 58310243 56940512 -2.35% garbage.BenchmarkTree2 31471700 31289400 -0.58% garbage.BenchmarkTree2-2 21536800 21086300 -2.09% garbage.BenchmarkTree2-4 11074700 10880000 -1.76% garbage.BenchmarkTree2-8 7568600 7351400 -2.87% garbage.BenchmarkTree2LastPause 314664000 312840000 -0.58% garbage.BenchmarkTree2LastPause-2 215319000 210815000 -2.09% garbage.BenchmarkTree2LastPause-4 110698000 108751000 -1.76% garbage.BenchmarkTree2LastPause-8 75635000 73463000 -2.87% garbage.BenchmarkTree2Pause 174280857 173147571 -0.65% garbage.BenchmarkTree2Pause-2 131332714 129665761 -1.27% garbage.BenchmarkTree2Pause-4 93803095 93422904 -0.41% garbage.BenchmarkTree2Pause-8 86242333 85146761 -1.27% R=rsc CC=golang-dev https://golang.org/cl/5987045
2012-04-05 10:48:28 -06:00
} else {
2014-06-26 09:54:39 -06:00
g->m->gcstats.nsleep++;
runtime·usleep(100);
runtime: make GC stats per-M This is factored out part of: https://golang.org/cl/5279048/ (Parallel GC) benchmark old ns/op new ns/op delta garbage.BenchmarkParser 3999106750 3975026500 -0.60% garbage.BenchmarkParser-2 3720553750 3719196500 -0.04% garbage.BenchmarkParser-4 3502857000 3474980500 -0.80% garbage.BenchmarkParser-8 3375448000 3341310500 -1.01% garbage.BenchmarkParserLastPause 329401000 324097000 -1.61% garbage.BenchmarkParserLastPause-2 208953000 214222000 +2.52% garbage.BenchmarkParserLastPause-4 110933000 111656000 +0.65% garbage.BenchmarkParserLastPause-8 71969000 78230000 +8.70% garbage.BenchmarkParserPause 230808842 197237400 -14.55% garbage.BenchmarkParserPause-2 123674365 125197595 +1.23% garbage.BenchmarkParserPause-4 80518525 85710333 +6.45% garbage.BenchmarkParserPause-8 58310243 56940512 -2.35% garbage.BenchmarkTree2 31471700 31289400 -0.58% garbage.BenchmarkTree2-2 21536800 21086300 -2.09% garbage.BenchmarkTree2-4 11074700 10880000 -1.76% garbage.BenchmarkTree2-8 7568600 7351400 -2.87% garbage.BenchmarkTree2LastPause 314664000 312840000 -0.58% garbage.BenchmarkTree2LastPause-2 215319000 210815000 -2.09% garbage.BenchmarkTree2LastPause-4 110698000 108751000 -1.76% garbage.BenchmarkTree2LastPause-8 75635000 73463000 -2.87% garbage.BenchmarkTree2Pause 174280857 173147571 -0.65% garbage.BenchmarkTree2Pause-2 131332714 129665761 -1.27% garbage.BenchmarkTree2Pause-4 93803095 93422904 -0.41% garbage.BenchmarkTree2Pause-8 86242333 85146761 -1.27% R=rsc CC=golang-dev https://golang.org/cl/5987045
2012-04-05 10:48:28 -06:00
}
}
}
static Workbuf*
handoff(Workbuf *b)
{
int32 n;
Workbuf *b1;
// Make new buffer with half of b's pointers.
b1 = getempty(nil);
n = b->nobj/2;
b->nobj -= n;
b1->nobj = n;
runtime·memmove(b1->obj, b->obj+b->nobj, n*sizeof b1->obj[0]);
2014-06-26 09:54:39 -06:00
g->m->gcstats.nhandoff++;
g->m->gcstats.nhandoffcnt += n;
// Put b on full list - let first half of b get stolen.
runtime: faster GC mark phase Also bump MaxGcproc to 8. benchmark old ns/op new ns/op delta Parser 3796323000 3763880000 -0.85% Parser-2 3591752500 3518560250 -2.04% Parser-4 3423825250 3334955250 -2.60% Parser-8 3304585500 3267014750 -1.14% Parser-16 3313615750 3286160500 -0.83% Tree 984128500 942501166 -4.23% Tree-2 932564444 883266222 -5.29% Tree-4 835831000 799912777 -4.30% Tree-8 819238500 789717333 -3.73% Tree-16 880837833 837840055 -5.13% Tree2 604698100 579716900 -4.13% Tree2-2 372414500 356765200 -4.20% Tree2-4 187488100 177455900 -5.56% Tree2-8 136315300 102086700 -25.11% Tree2-16 93725900 76705800 -22.18% ParserPause 157441210 166202783 +5.56% ParserPause-2 93842650 85199900 -9.21% ParserPause-4 56844404 53535684 -5.82% ParserPause-8 35739446 30767613 -16.15% ParserPause-16 32718255 27212441 -16.83% TreePause 29610557 29787725 +0.60% TreePause-2 24001659 20674421 -13.86% TreePause-4 15114887 12842781 -15.03% TreePause-8 13128725 10741747 -22.22% TreePause-16 16131360 12506901 -22.47% Tree2Pause 2673350920 2651045280 -0.83% Tree2Pause-2 1796999200 1709350040 -4.88% Tree2Pause-4 1163553320 1090706480 -6.67% Tree2Pause-8 987032520 858916360 -25.11% Tree2Pause-16 864758560 809567480 -6.81% ParserLastPause 280537000 289047000 +3.03% ParserLastPause-2 183030000 166748000 -8.90% ParserLastPause-4 105817000 91552000 -13.48% ParserLastPause-8 65127000 53288000 -18.18% ParserLastPause-16 45258000 38334000 -15.30% TreeLastPause 45072000 51449000 +12.39% TreeLastPause-2 39269000 37866000 -3.57% TreeLastPause-4 23564000 20649000 -12.37% TreeLastPause-8 20881000 15807000 -24.30% TreeLastPause-16 23297000 17309000 -25.70% Tree2LastPause 6046912000 5797120000 -4.13% Tree2LastPause-2 3724034000 3567592000 -4.20% Tree2LastPause-4 1874831000 1774524000 -5.65% Tree2LastPause-8 1363108000 1020809000 -12.79% Tree2LastPause-16 937208000 767019000 -22.18% R=rsc, 0xe2.0x9a.0x9b CC=golang-dev https://golang.org/cl/6223050
2012-05-24 00:55:50 -06:00
runtime·lfstackpush(&work.full, &b->node);
return b1;
}
2014-04-02 14:49:27 -06:00
BitVector
runtime·stackmapdata(StackMap *stackmap, int32 n)
{
2014-04-02 14:49:27 -06:00
if(n < 0 || n >= stackmap->n)
runtime·throw("stackmapdata: index out of range");
2014-04-02 14:49:27 -06:00
return (BitVector){stackmap->nbit, stackmap->data + n*((stackmap->nbit+31)/32)};
}
// Scan a stack frame: local variables and function arguments/results.
static bool
scanframe(Stkframe *frame, void *unused)
{
Func *f;
StackMap *stackmap;
2014-04-02 14:49:27 -06:00
BitVector bv;
uintptr size;
uintptr targetpc;
int32 pcdata;
USED(unused);
f = frame->fn;
targetpc = frame->continpc;
if(targetpc == 0) {
// Frame is dead.
return true;
}
if(Debug > 1)
runtime·printf("scanframe %s\n", runtime·funcname(f));
if(targetpc != f->entry)
targetpc--;
pcdata = runtime·pcdatavalue(f, PCDATA_StackMapIndex, targetpc);
if(pcdata == -1) {
// We do not have a valid pcdata value but there might be a
// stackmap for this function. It is likely that we are looking
// at the function prologue, assume so and hope for the best.
pcdata = 0;
}
// Scan local variables if stack frame has been allocated.
// Use pointer information if known.
stackmap = runtime·funcdata(f, FUNCDATA_LocalsPointerMaps);
if(stackmap == nil) {
// No locals information, scan everything.
size = frame->varp - (byte*)frame->sp;
if(Debug > 2)
runtime·printf("frame %s unsized locals %p+%p\n", runtime·funcname(f), frame->varp-size, size);
scanblock(frame->varp - size, size, ScanConservatively);
} else if(stackmap->n < 0) {
// Locals size information, scan just the locals.
size = -stackmap->n;
if(Debug > 2)
runtime·printf("frame %s conservative locals %p+%p\n", runtime·funcname(f), frame->varp-size, size);
scanblock(frame->varp - size, size, ScanConservatively);
} else if(stackmap->n > 0) {
// Locals bitmap information, scan just the pointers in locals.
if(pcdata < 0 || pcdata >= stackmap->n) {
// don't know where we are
runtime·printf("pcdata is %d and %d stack map entries for %s (targetpc=%p)\n",
pcdata, stackmap->n, runtime·funcname(f), targetpc);
runtime·throw("scanframe: bad symbol table");
}
bv = runtime·stackmapdata(stackmap, pcdata);
size = (bv.n * PtrSize) / BitsPerPointer;
scanblock(frame->varp - size, bv.n/BitsPerPointer*PtrSize, (byte*)bv.data);
}
// Scan arguments.
// Use pointer information if known.
stackmap = runtime·funcdata(f, FUNCDATA_ArgsPointerMaps);
if(stackmap != nil) {
bv = runtime·stackmapdata(stackmap, pcdata);
scanblock(frame->argp, bv.n/BitsPerPointer*PtrSize, (byte*)bv.data);
reflect, runtime: fix crash in GC due to reflect.call + precise GC Given type Outer struct { *Inner ... } the compiler generates the implementation of (*Outer).M dispatching to the embedded Inner. The implementation is logically: func (p *Outer) M() { (p.Inner).M() } but since the only change here is the replacement of one pointer receiver with another, the actual generated code overwrites the original receiver with the p.Inner pointer and then jumps to the M method expecting the *Inner receiver. During reflect.Value.Call, we create an argument frame and the associated data structures to describe it to the garbage collector, populate the frame, call reflect.call to run a function call using that frame, and then copy the results back out of the frame. The reflect.call function does a memmove of the frame structure onto the stack (to set up the inputs), runs the call, and the memmoves the stack back to the frame structure (to preserve the outputs). Originally reflect.call did not distinguish inputs from outputs: both memmoves were for the full stack frame. However, in the case where the called function was one of these wrappers, the rewritten receiver is almost certainly a different type than the original receiver. This is not a problem on the stack, where we use the program counter to determine the type information and understand that during (*Outer).M the receiver is an *Outer while during (*Inner).M the receiver in the same memory word is now an *Inner. But in the statically typed argument frame created by reflect, the receiver is always an *Outer. Copying the modified receiver pointer off the stack into the frame will store an *Inner there, and then if a garbage collection happens to scan that argument frame before it is discarded, it will scan the *Inner memory as if it were an *Outer. If the two have different memory layouts, the collection will intepret the memory incorrectly. Fix by only copying back the results. Fixes #7725. LGTM=khr R=khr CC=dave, golang-codereviews https://golang.org/cl/85180043
2014-04-08 09:11:35 -06:00
} else {
if(Debug > 2)
runtime·printf("frame %s conservative args %p+%p\n", runtime·funcname(f), frame->argp, (uintptr)frame->arglen);
scanblock(frame->argp, frame->arglen, ScanConservatively);
reflect, runtime: fix crash in GC due to reflect.call + precise GC Given type Outer struct { *Inner ... } the compiler generates the implementation of (*Outer).M dispatching to the embedded Inner. The implementation is logically: func (p *Outer) M() { (p.Inner).M() } but since the only change here is the replacement of one pointer receiver with another, the actual generated code overwrites the original receiver with the p.Inner pointer and then jumps to the M method expecting the *Inner receiver. During reflect.Value.Call, we create an argument frame and the associated data structures to describe it to the garbage collector, populate the frame, call reflect.call to run a function call using that frame, and then copy the results back out of the frame. The reflect.call function does a memmove of the frame structure onto the stack (to set up the inputs), runs the call, and the memmoves the stack back to the frame structure (to preserve the outputs). Originally reflect.call did not distinguish inputs from outputs: both memmoves were for the full stack frame. However, in the case where the called function was one of these wrappers, the rewritten receiver is almost certainly a different type than the original receiver. This is not a problem on the stack, where we use the program counter to determine the type information and understand that during (*Outer).M the receiver is an *Outer while during (*Inner).M the receiver in the same memory word is now an *Inner. But in the statically typed argument frame created by reflect, the receiver is always an *Outer. Copying the modified receiver pointer off the stack into the frame will store an *Inner there, and then if a garbage collection happens to scan that argument frame before it is discarded, it will scan the *Inner memory as if it were an *Outer. If the two have different memory layouts, the collection will intepret the memory incorrectly. Fix by only copying back the results. Fixes #7725. LGTM=khr R=khr CC=dave, golang-codereviews https://golang.org/cl/85180043
2014-04-08 09:11:35 -06:00
}
return true;
}
static void
scanstack(G *gp)
{
M *mp;
int32 n;
Stktop *stk;
uintptr sp, guard;
switch(gp->status){
default:
runtime·printf("unexpected G.status %d (goroutine %p %D)\n", gp->status, gp, gp->goid);
runtime·throw("mark - bad status");
case Gdead:
return;
case Grunning:
runtime·throw("mark - world not stopped");
case Grunnable:
case Gsyscall:
case Gwaiting:
break;
}
if(gp == g)
runtime·throw("can't scan our own stack");
if((mp = gp->m) != nil && mp->helpgc)
runtime·throw("can't scan gchelper stack");
if(gp->syscallstack != (uintptr)nil) {
// Scanning another goroutine that is about to enter or might
// have just exited a system call. It may be executing code such
// as schedlock and may have needed to start a new stack segment.
// Use the stack segment and stack pointer at the time of
// the system call instead, since that won't change underfoot.
sp = gp->syscallsp;
stk = (Stktop*)gp->syscallstack;
guard = gp->syscallguard;
} else {
// Scanning another goroutine's stack.
// The goroutine is usually asleep (the world is stopped).
sp = gp->sched.sp;
stk = (Stktop*)gp->stackbase;
guard = gp->stackguard;
}
if(ScanStackByFrames) {
USED(sp);
USED(stk);
USED(guard);
runtime·gentraceback(~(uintptr)0, ~(uintptr)0, 0, gp, 0, nil, 0x7fffffff, scanframe, nil, false);
} else {
n = 0;
while(stk) {
if(sp < guard-StackGuard || (uintptr)stk < sp) {
runtime·printf("scanstack inconsistent: g%D#%d sp=%p not in [%p,%p]\n", gp->goid, n, sp, guard-StackGuard, stk);
runtime·throw("scanstack");
}
reflect, runtime: fix crash in GC due to reflect.call + precise GC Given type Outer struct { *Inner ... } the compiler generates the implementation of (*Outer).M dispatching to the embedded Inner. The implementation is logically: func (p *Outer) M() { (p.Inner).M() } but since the only change here is the replacement of one pointer receiver with another, the actual generated code overwrites the original receiver with the p.Inner pointer and then jumps to the M method expecting the *Inner receiver. During reflect.Value.Call, we create an argument frame and the associated data structures to describe it to the garbage collector, populate the frame, call reflect.call to run a function call using that frame, and then copy the results back out of the frame. The reflect.call function does a memmove of the frame structure onto the stack (to set up the inputs), runs the call, and the memmoves the stack back to the frame structure (to preserve the outputs). Originally reflect.call did not distinguish inputs from outputs: both memmoves were for the full stack frame. However, in the case where the called function was one of these wrappers, the rewritten receiver is almost certainly a different type than the original receiver. This is not a problem on the stack, where we use the program counter to determine the type information and understand that during (*Outer).M the receiver is an *Outer while during (*Inner).M the receiver in the same memory word is now an *Inner. But in the statically typed argument frame created by reflect, the receiver is always an *Outer. Copying the modified receiver pointer off the stack into the frame will store an *Inner there, and then if a garbage collection happens to scan that argument frame before it is discarded, it will scan the *Inner memory as if it were an *Outer. If the two have different memory layouts, the collection will intepret the memory incorrectly. Fix by only copying back the results. Fixes #7725. LGTM=khr R=khr CC=dave, golang-codereviews https://golang.org/cl/85180043
2014-04-08 09:11:35 -06:00
if(Debug > 2)
runtime·printf("conservative stack %p+%p\n", (byte*)sp, (uintptr)stk-sp);
scanblock((byte*)sp, (uintptr)stk - sp, ScanConservatively);
sp = stk->gobuf.sp;
guard = stk->stackguard;
stk = (Stktop*)stk->stackbase;
n++;
}
}
}
void
runtime·queuefinalizer(byte *p, FuncVal *fn, uintptr nret, Type *fint, PtrType *ot)
{
FinBlock *block;
Finalizer *f;
runtime·lock(&finlock);
if(finq == nil || finq->cnt == finq->cap) {
if(finc == nil) {
2014-01-30 02:28:19 -07:00
finc = runtime·persistentalloc(FinBlockSize, 0, &mstats.gc_sys);
finc->cap = (FinBlockSize - sizeof(FinBlock)) / sizeof(Finalizer) + 1;
finc->alllink = allfin;
allfin = finc;
}
block = finc;
finc = block->next;
block->next = finq;
finq = block;
}
f = &finq->fin[finq->cnt];
finq->cnt++;
f->fn = fn;
f->nret = nret;
f->fint = fint;
f->ot = ot;
f->arg = p;
runtime·fingwake = true;
runtime·unlock(&finlock);
runtime: concurrent GC sweep Moves sweep phase out of stoptheworld by adding background sweeper goroutine and lazy on-demand sweeping. It turned out to be somewhat trickier than I expected, because there is no point in time when we know size of live heap nor consistent number of mallocs and frees. So everything related to next_gc, mprof, memstats, etc becomes trickier. At the end of GC next_gc is conservatively set to heap_alloc*GOGC, which is much larger than real value. But after every sweep next_gc is decremented by freed*GOGC. So when everything is swept next_gc becomes what it should be. For mprof I had to introduce 3-generation scheme (allocs, revent_allocs, prev_allocs), because by the end of GC we know number of frees for the *previous* GC. Significant caution is required to not cross yet-unknown real value of next_gc. This is achieved by 2 means: 1. Whenever I allocate a span from MCentral, I sweep a span in that MCentral. 2. Whenever I allocate N pages from MHeap, I sweep until at least N pages are returned to heap. This provides quite strong guarantees that heap does not grow when it should now. http-1 allocated 7036 7033 -0.04% allocs 60 60 +0.00% cputime 51050 46700 -8.52% gc-pause-one 34060569 1777993 -94.78% gc-pause-total 2554 133 -94.79% latency-50 178448 170926 -4.22% latency-95 284350 198294 -30.26% latency-99 345191 220652 -36.08% rss 101564416 101007360 -0.55% sys-gc 6606832 6541296 -0.99% sys-heap 88801280 87752704 -1.18% sys-other 7334208 7405928 +0.98% sys-stack 524288 524288 +0.00% sys-total 103266608 102224216 -1.01% time 50339 46533 -7.56% virtual-mem 292990976 293728256 +0.25% garbage-1 allocated 2983818 2990889 +0.24% allocs 62880 62902 +0.03% cputime 16480000 16190000 -1.76% gc-pause-one 828462467 487875135 -41.11% gc-pause-total 4142312 2439375 -41.11% rss 1151709184 1153712128 +0.17% sys-gc 66068352 66068352 +0.00% sys-heap 1039728640 1039728640 +0.00% sys-other 37776064 40770176 +7.93% sys-stack 8781824 8781824 +0.00% sys-total 1152354880 1155348992 +0.26% time 16496998 16199876 -1.80% virtual-mem 1409564672 1402281984 -0.52% LGTM=rsc R=golang-codereviews, sameer, rsc, iant, jeremyjackins, gobot CC=golang-codereviews, khr https://golang.org/cl/46430043
2014-02-12 11:16:42 -07:00
}
void
runtime·iterate_finq(void (*callback)(FuncVal*, byte*, uintptr, Type*, PtrType*))
{
FinBlock *fb;
Finalizer *f;
uintptr i;
for(fb = allfin; fb; fb = fb->alllink) {
for(i = 0; i < fb->cnt; i++) {
f = &fb->fin[i];
callback(f->fn, f->arg, f->nret, f->fint, f->ot);
}
}
}
runtime: concurrent GC sweep Moves sweep phase out of stoptheworld by adding background sweeper goroutine and lazy on-demand sweeping. It turned out to be somewhat trickier than I expected, because there is no point in time when we know size of live heap nor consistent number of mallocs and frees. So everything related to next_gc, mprof, memstats, etc becomes trickier. At the end of GC next_gc is conservatively set to heap_alloc*GOGC, which is much larger than real value. But after every sweep next_gc is decremented by freed*GOGC. So when everything is swept next_gc becomes what it should be. For mprof I had to introduce 3-generation scheme (allocs, revent_allocs, prev_allocs), because by the end of GC we know number of frees for the *previous* GC. Significant caution is required to not cross yet-unknown real value of next_gc. This is achieved by 2 means: 1. Whenever I allocate a span from MCentral, I sweep a span in that MCentral. 2. Whenever I allocate N pages from MHeap, I sweep until at least N pages are returned to heap. This provides quite strong guarantees that heap does not grow when it should now. http-1 allocated 7036 7033 -0.04% allocs 60 60 +0.00% cputime 51050 46700 -8.52% gc-pause-one 34060569 1777993 -94.78% gc-pause-total 2554 133 -94.79% latency-50 178448 170926 -4.22% latency-95 284350 198294 -30.26% latency-99 345191 220652 -36.08% rss 101564416 101007360 -0.55% sys-gc 6606832 6541296 -0.99% sys-heap 88801280 87752704 -1.18% sys-other 7334208 7405928 +0.98% sys-stack 524288 524288 +0.00% sys-total 103266608 102224216 -1.01% time 50339 46533 -7.56% virtual-mem 292990976 293728256 +0.25% garbage-1 allocated 2983818 2990889 +0.24% allocs 62880 62902 +0.03% cputime 16480000 16190000 -1.76% gc-pause-one 828462467 487875135 -41.11% gc-pause-total 4142312 2439375 -41.11% rss 1151709184 1153712128 +0.17% sys-gc 66068352 66068352 +0.00% sys-heap 1039728640 1039728640 +0.00% sys-other 37776064 40770176 +7.93% sys-stack 8781824 8781824 +0.00% sys-total 1152354880 1155348992 +0.26% time 16496998 16199876 -1.80% virtual-mem 1409564672 1402281984 -0.52% LGTM=rsc R=golang-codereviews, sameer, rsc, iant, jeremyjackins, gobot CC=golang-codereviews, khr https://golang.org/cl/46430043
2014-02-12 11:16:42 -07:00
void
runtime·MSpan_EnsureSwept(MSpan *s)
{
uint32 sg;
// Caller must disable preemption.
// Otherwise when this function returns the span can become unswept again
// (if GC is triggered on another goroutine).
2014-06-26 09:54:39 -06:00
if(g->m->locks == 0 && g->m->mallocing == 0 && g != g->m->g0)
runtime·throw("MSpan_EnsureSwept: m is not locked");
runtime: concurrent GC sweep Moves sweep phase out of stoptheworld by adding background sweeper goroutine and lazy on-demand sweeping. It turned out to be somewhat trickier than I expected, because there is no point in time when we know size of live heap nor consistent number of mallocs and frees. So everything related to next_gc, mprof, memstats, etc becomes trickier. At the end of GC next_gc is conservatively set to heap_alloc*GOGC, which is much larger than real value. But after every sweep next_gc is decremented by freed*GOGC. So when everything is swept next_gc becomes what it should be. For mprof I had to introduce 3-generation scheme (allocs, revent_allocs, prev_allocs), because by the end of GC we know number of frees for the *previous* GC. Significant caution is required to not cross yet-unknown real value of next_gc. This is achieved by 2 means: 1. Whenever I allocate a span from MCentral, I sweep a span in that MCentral. 2. Whenever I allocate N pages from MHeap, I sweep until at least N pages are returned to heap. This provides quite strong guarantees that heap does not grow when it should now. http-1 allocated 7036 7033 -0.04% allocs 60 60 +0.00% cputime 51050 46700 -8.52% gc-pause-one 34060569 1777993 -94.78% gc-pause-total 2554 133 -94.79% latency-50 178448 170926 -4.22% latency-95 284350 198294 -30.26% latency-99 345191 220652 -36.08% rss 101564416 101007360 -0.55% sys-gc 6606832 6541296 -0.99% sys-heap 88801280 87752704 -1.18% sys-other 7334208 7405928 +0.98% sys-stack 524288 524288 +0.00% sys-total 103266608 102224216 -1.01% time 50339 46533 -7.56% virtual-mem 292990976 293728256 +0.25% garbage-1 allocated 2983818 2990889 +0.24% allocs 62880 62902 +0.03% cputime 16480000 16190000 -1.76% gc-pause-one 828462467 487875135 -41.11% gc-pause-total 4142312 2439375 -41.11% rss 1151709184 1153712128 +0.17% sys-gc 66068352 66068352 +0.00% sys-heap 1039728640 1039728640 +0.00% sys-other 37776064 40770176 +7.93% sys-stack 8781824 8781824 +0.00% sys-total 1152354880 1155348992 +0.26% time 16496998 16199876 -1.80% virtual-mem 1409564672 1402281984 -0.52% LGTM=rsc R=golang-codereviews, sameer, rsc, iant, jeremyjackins, gobot CC=golang-codereviews, khr https://golang.org/cl/46430043
2014-02-12 11:16:42 -07:00
sg = runtime·mheap.sweepgen;
if(runtime·atomicload(&s->sweepgen) == sg)
return;
if(runtime·cas(&s->sweepgen, sg-2, sg-1)) {
runtime·MSpan_Sweep(s, false);
runtime: concurrent GC sweep Moves sweep phase out of stoptheworld by adding background sweeper goroutine and lazy on-demand sweeping. It turned out to be somewhat trickier than I expected, because there is no point in time when we know size of live heap nor consistent number of mallocs and frees. So everything related to next_gc, mprof, memstats, etc becomes trickier. At the end of GC next_gc is conservatively set to heap_alloc*GOGC, which is much larger than real value. But after every sweep next_gc is decremented by freed*GOGC. So when everything is swept next_gc becomes what it should be. For mprof I had to introduce 3-generation scheme (allocs, revent_allocs, prev_allocs), because by the end of GC we know number of frees for the *previous* GC. Significant caution is required to not cross yet-unknown real value of next_gc. This is achieved by 2 means: 1. Whenever I allocate a span from MCentral, I sweep a span in that MCentral. 2. Whenever I allocate N pages from MHeap, I sweep until at least N pages are returned to heap. This provides quite strong guarantees that heap does not grow when it should now. http-1 allocated 7036 7033 -0.04% allocs 60 60 +0.00% cputime 51050 46700 -8.52% gc-pause-one 34060569 1777993 -94.78% gc-pause-total 2554 133 -94.79% latency-50 178448 170926 -4.22% latency-95 284350 198294 -30.26% latency-99 345191 220652 -36.08% rss 101564416 101007360 -0.55% sys-gc 6606832 6541296 -0.99% sys-heap 88801280 87752704 -1.18% sys-other 7334208 7405928 +0.98% sys-stack 524288 524288 +0.00% sys-total 103266608 102224216 -1.01% time 50339 46533 -7.56% virtual-mem 292990976 293728256 +0.25% garbage-1 allocated 2983818 2990889 +0.24% allocs 62880 62902 +0.03% cputime 16480000 16190000 -1.76% gc-pause-one 828462467 487875135 -41.11% gc-pause-total 4142312 2439375 -41.11% rss 1151709184 1153712128 +0.17% sys-gc 66068352 66068352 +0.00% sys-heap 1039728640 1039728640 +0.00% sys-other 37776064 40770176 +7.93% sys-stack 8781824 8781824 +0.00% sys-total 1152354880 1155348992 +0.26% time 16496998 16199876 -1.80% virtual-mem 1409564672 1402281984 -0.52% LGTM=rsc R=golang-codereviews, sameer, rsc, iant, jeremyjackins, gobot CC=golang-codereviews, khr https://golang.org/cl/46430043
2014-02-12 11:16:42 -07:00
return;
}
// unfortunate condition, and we don't have efficient means to wait
while(runtime·atomicload(&s->sweepgen) != sg)
runtime·osyield();
}
// Sweep frees or collects finalizers for blocks not marked in the mark phase.
// It clears the mark bits in preparation for the next GC round.
runtime: concurrent GC sweep Moves sweep phase out of stoptheworld by adding background sweeper goroutine and lazy on-demand sweeping. It turned out to be somewhat trickier than I expected, because there is no point in time when we know size of live heap nor consistent number of mallocs and frees. So everything related to next_gc, mprof, memstats, etc becomes trickier. At the end of GC next_gc is conservatively set to heap_alloc*GOGC, which is much larger than real value. But after every sweep next_gc is decremented by freed*GOGC. So when everything is swept next_gc becomes what it should be. For mprof I had to introduce 3-generation scheme (allocs, revent_allocs, prev_allocs), because by the end of GC we know number of frees for the *previous* GC. Significant caution is required to not cross yet-unknown real value of next_gc. This is achieved by 2 means: 1. Whenever I allocate a span from MCentral, I sweep a span in that MCentral. 2. Whenever I allocate N pages from MHeap, I sweep until at least N pages are returned to heap. This provides quite strong guarantees that heap does not grow when it should now. http-1 allocated 7036 7033 -0.04% allocs 60 60 +0.00% cputime 51050 46700 -8.52% gc-pause-one 34060569 1777993 -94.78% gc-pause-total 2554 133 -94.79% latency-50 178448 170926 -4.22% latency-95 284350 198294 -30.26% latency-99 345191 220652 -36.08% rss 101564416 101007360 -0.55% sys-gc 6606832 6541296 -0.99% sys-heap 88801280 87752704 -1.18% sys-other 7334208 7405928 +0.98% sys-stack 524288 524288 +0.00% sys-total 103266608 102224216 -1.01% time 50339 46533 -7.56% virtual-mem 292990976 293728256 +0.25% garbage-1 allocated 2983818 2990889 +0.24% allocs 62880 62902 +0.03% cputime 16480000 16190000 -1.76% gc-pause-one 828462467 487875135 -41.11% gc-pause-total 4142312 2439375 -41.11% rss 1151709184 1153712128 +0.17% sys-gc 66068352 66068352 +0.00% sys-heap 1039728640 1039728640 +0.00% sys-other 37776064 40770176 +7.93% sys-stack 8781824 8781824 +0.00% sys-total 1152354880 1155348992 +0.26% time 16496998 16199876 -1.80% virtual-mem 1409564672 1402281984 -0.52% LGTM=rsc R=golang-codereviews, sameer, rsc, iant, jeremyjackins, gobot CC=golang-codereviews, khr https://golang.org/cl/46430043
2014-02-12 11:16:42 -07:00
// Returns true if the span was returned to heap.
// If preserve=true, don't return it to heap nor relink in MCentral lists;
// caller takes care of it.
runtime: concurrent GC sweep Moves sweep phase out of stoptheworld by adding background sweeper goroutine and lazy on-demand sweeping. It turned out to be somewhat trickier than I expected, because there is no point in time when we know size of live heap nor consistent number of mallocs and frees. So everything related to next_gc, mprof, memstats, etc becomes trickier. At the end of GC next_gc is conservatively set to heap_alloc*GOGC, which is much larger than real value. But after every sweep next_gc is decremented by freed*GOGC. So when everything is swept next_gc becomes what it should be. For mprof I had to introduce 3-generation scheme (allocs, revent_allocs, prev_allocs), because by the end of GC we know number of frees for the *previous* GC. Significant caution is required to not cross yet-unknown real value of next_gc. This is achieved by 2 means: 1. Whenever I allocate a span from MCentral, I sweep a span in that MCentral. 2. Whenever I allocate N pages from MHeap, I sweep until at least N pages are returned to heap. This provides quite strong guarantees that heap does not grow when it should now. http-1 allocated 7036 7033 -0.04% allocs 60 60 +0.00% cputime 51050 46700 -8.52% gc-pause-one 34060569 1777993 -94.78% gc-pause-total 2554 133 -94.79% latency-50 178448 170926 -4.22% latency-95 284350 198294 -30.26% latency-99 345191 220652 -36.08% rss 101564416 101007360 -0.55% sys-gc 6606832 6541296 -0.99% sys-heap 88801280 87752704 -1.18% sys-other 7334208 7405928 +0.98% sys-stack 524288 524288 +0.00% sys-total 103266608 102224216 -1.01% time 50339 46533 -7.56% virtual-mem 292990976 293728256 +0.25% garbage-1 allocated 2983818 2990889 +0.24% allocs 62880 62902 +0.03% cputime 16480000 16190000 -1.76% gc-pause-one 828462467 487875135 -41.11% gc-pause-total 4142312 2439375 -41.11% rss 1151709184 1153712128 +0.17% sys-gc 66068352 66068352 +0.00% sys-heap 1039728640 1039728640 +0.00% sys-other 37776064 40770176 +7.93% sys-stack 8781824 8781824 +0.00% sys-total 1152354880 1155348992 +0.26% time 16496998 16199876 -1.80% virtual-mem 1409564672 1402281984 -0.52% LGTM=rsc R=golang-codereviews, sameer, rsc, iant, jeremyjackins, gobot CC=golang-codereviews, khr https://golang.org/cl/46430043
2014-02-12 11:16:42 -07:00
bool
runtime·MSpan_Sweep(MSpan *s, bool preserve)
{
int32 cl, n, npages, nfree;
uintptr size, off, step;
uint32 sweepgen;
byte *p, *bitp, shift, xbits, bits;
MCache *c;
byte *arena_start;
MLink head, *end, *link;
Special *special, **specialp, *y;
runtime: concurrent GC sweep Moves sweep phase out of stoptheworld by adding background sweeper goroutine and lazy on-demand sweeping. It turned out to be somewhat trickier than I expected, because there is no point in time when we know size of live heap nor consistent number of mallocs and frees. So everything related to next_gc, mprof, memstats, etc becomes trickier. At the end of GC next_gc is conservatively set to heap_alloc*GOGC, which is much larger than real value. But after every sweep next_gc is decremented by freed*GOGC. So when everything is swept next_gc becomes what it should be. For mprof I had to introduce 3-generation scheme (allocs, revent_allocs, prev_allocs), because by the end of GC we know number of frees for the *previous* GC. Significant caution is required to not cross yet-unknown real value of next_gc. This is achieved by 2 means: 1. Whenever I allocate a span from MCentral, I sweep a span in that MCentral. 2. Whenever I allocate N pages from MHeap, I sweep until at least N pages are returned to heap. This provides quite strong guarantees that heap does not grow when it should now. http-1 allocated 7036 7033 -0.04% allocs 60 60 +0.00% cputime 51050 46700 -8.52% gc-pause-one 34060569 1777993 -94.78% gc-pause-total 2554 133 -94.79% latency-50 178448 170926 -4.22% latency-95 284350 198294 -30.26% latency-99 345191 220652 -36.08% rss 101564416 101007360 -0.55% sys-gc 6606832 6541296 -0.99% sys-heap 88801280 87752704 -1.18% sys-other 7334208 7405928 +0.98% sys-stack 524288 524288 +0.00% sys-total 103266608 102224216 -1.01% time 50339 46533 -7.56% virtual-mem 292990976 293728256 +0.25% garbage-1 allocated 2983818 2990889 +0.24% allocs 62880 62902 +0.03% cputime 16480000 16190000 -1.76% gc-pause-one 828462467 487875135 -41.11% gc-pause-total 4142312 2439375 -41.11% rss 1151709184 1153712128 +0.17% sys-gc 66068352 66068352 +0.00% sys-heap 1039728640 1039728640 +0.00% sys-other 37776064 40770176 +7.93% sys-stack 8781824 8781824 +0.00% sys-total 1152354880 1155348992 +0.26% time 16496998 16199876 -1.80% virtual-mem 1409564672 1402281984 -0.52% LGTM=rsc R=golang-codereviews, sameer, rsc, iant, jeremyjackins, gobot CC=golang-codereviews, khr https://golang.org/cl/46430043
2014-02-12 11:16:42 -07:00
bool res, sweepgenset;
// It's critical that we enter this function with preemption disabled,
// GC must not start while we are in the middle of this function.
2014-06-26 09:54:39 -06:00
if(g->m->locks == 0 && g->m->mallocing == 0 && g != g->m->g0)
runtime·throw("MSpan_Sweep: m is not locked");
sweepgen = runtime·mheap.sweepgen;
if(s->state != MSpanInUse || s->sweepgen != sweepgen-1) {
runtime: concurrent GC sweep Moves sweep phase out of stoptheworld by adding background sweeper goroutine and lazy on-demand sweeping. It turned out to be somewhat trickier than I expected, because there is no point in time when we know size of live heap nor consistent number of mallocs and frees. So everything related to next_gc, mprof, memstats, etc becomes trickier. At the end of GC next_gc is conservatively set to heap_alloc*GOGC, which is much larger than real value. But after every sweep next_gc is decremented by freed*GOGC. So when everything is swept next_gc becomes what it should be. For mprof I had to introduce 3-generation scheme (allocs, revent_allocs, prev_allocs), because by the end of GC we know number of frees for the *previous* GC. Significant caution is required to not cross yet-unknown real value of next_gc. This is achieved by 2 means: 1. Whenever I allocate a span from MCentral, I sweep a span in that MCentral. 2. Whenever I allocate N pages from MHeap, I sweep until at least N pages are returned to heap. This provides quite strong guarantees that heap does not grow when it should now. http-1 allocated 7036 7033 -0.04% allocs 60 60 +0.00% cputime 51050 46700 -8.52% gc-pause-one 34060569 1777993 -94.78% gc-pause-total 2554 133 -94.79% latency-50 178448 170926 -4.22% latency-95 284350 198294 -30.26% latency-99 345191 220652 -36.08% rss 101564416 101007360 -0.55% sys-gc 6606832 6541296 -0.99% sys-heap 88801280 87752704 -1.18% sys-other 7334208 7405928 +0.98% sys-stack 524288 524288 +0.00% sys-total 103266608 102224216 -1.01% time 50339 46533 -7.56% virtual-mem 292990976 293728256 +0.25% garbage-1 allocated 2983818 2990889 +0.24% allocs 62880 62902 +0.03% cputime 16480000 16190000 -1.76% gc-pause-one 828462467 487875135 -41.11% gc-pause-total 4142312 2439375 -41.11% rss 1151709184 1153712128 +0.17% sys-gc 66068352 66068352 +0.00% sys-heap 1039728640 1039728640 +0.00% sys-other 37776064 40770176 +7.93% sys-stack 8781824 8781824 +0.00% sys-total 1152354880 1155348992 +0.26% time 16496998 16199876 -1.80% virtual-mem 1409564672 1402281984 -0.52% LGTM=rsc R=golang-codereviews, sameer, rsc, iant, jeremyjackins, gobot CC=golang-codereviews, khr https://golang.org/cl/46430043
2014-02-12 11:16:42 -07:00
runtime·printf("MSpan_Sweep: state=%d sweepgen=%d mheap.sweepgen=%d\n",
s->state, s->sweepgen, sweepgen);
runtime: concurrent GC sweep Moves sweep phase out of stoptheworld by adding background sweeper goroutine and lazy on-demand sweeping. It turned out to be somewhat trickier than I expected, because there is no point in time when we know size of live heap nor consistent number of mallocs and frees. So everything related to next_gc, mprof, memstats, etc becomes trickier. At the end of GC next_gc is conservatively set to heap_alloc*GOGC, which is much larger than real value. But after every sweep next_gc is decremented by freed*GOGC. So when everything is swept next_gc becomes what it should be. For mprof I had to introduce 3-generation scheme (allocs, revent_allocs, prev_allocs), because by the end of GC we know number of frees for the *previous* GC. Significant caution is required to not cross yet-unknown real value of next_gc. This is achieved by 2 means: 1. Whenever I allocate a span from MCentral, I sweep a span in that MCentral. 2. Whenever I allocate N pages from MHeap, I sweep until at least N pages are returned to heap. This provides quite strong guarantees that heap does not grow when it should now. http-1 allocated 7036 7033 -0.04% allocs 60 60 +0.00% cputime 51050 46700 -8.52% gc-pause-one 34060569 1777993 -94.78% gc-pause-total 2554 133 -94.79% latency-50 178448 170926 -4.22% latency-95 284350 198294 -30.26% latency-99 345191 220652 -36.08% rss 101564416 101007360 -0.55% sys-gc 6606832 6541296 -0.99% sys-heap 88801280 87752704 -1.18% sys-other 7334208 7405928 +0.98% sys-stack 524288 524288 +0.00% sys-total 103266608 102224216 -1.01% time 50339 46533 -7.56% virtual-mem 292990976 293728256 +0.25% garbage-1 allocated 2983818 2990889 +0.24% allocs 62880 62902 +0.03% cputime 16480000 16190000 -1.76% gc-pause-one 828462467 487875135 -41.11% gc-pause-total 4142312 2439375 -41.11% rss 1151709184 1153712128 +0.17% sys-gc 66068352 66068352 +0.00% sys-heap 1039728640 1039728640 +0.00% sys-other 37776064 40770176 +7.93% sys-stack 8781824 8781824 +0.00% sys-total 1152354880 1155348992 +0.26% time 16496998 16199876 -1.80% virtual-mem 1409564672 1402281984 -0.52% LGTM=rsc R=golang-codereviews, sameer, rsc, iant, jeremyjackins, gobot CC=golang-codereviews, khr https://golang.org/cl/46430043
2014-02-12 11:16:42 -07:00
runtime·throw("MSpan_Sweep: bad span state");
}
arena_start = runtime·mheap.arena_start;
cl = s->sizeclass;
size = s->elemsize;
if(cl == 0) {
n = 1;
} else {
// Chunk full of small blocks.
npages = runtime·class_to_allocnpages[cl];
n = (npages << PageShift) / size;
}
runtime: concurrent GC sweep Moves sweep phase out of stoptheworld by adding background sweeper goroutine and lazy on-demand sweeping. It turned out to be somewhat trickier than I expected, because there is no point in time when we know size of live heap nor consistent number of mallocs and frees. So everything related to next_gc, mprof, memstats, etc becomes trickier. At the end of GC next_gc is conservatively set to heap_alloc*GOGC, which is much larger than real value. But after every sweep next_gc is decremented by freed*GOGC. So when everything is swept next_gc becomes what it should be. For mprof I had to introduce 3-generation scheme (allocs, revent_allocs, prev_allocs), because by the end of GC we know number of frees for the *previous* GC. Significant caution is required to not cross yet-unknown real value of next_gc. This is achieved by 2 means: 1. Whenever I allocate a span from MCentral, I sweep a span in that MCentral. 2. Whenever I allocate N pages from MHeap, I sweep until at least N pages are returned to heap. This provides quite strong guarantees that heap does not grow when it should now. http-1 allocated 7036 7033 -0.04% allocs 60 60 +0.00% cputime 51050 46700 -8.52% gc-pause-one 34060569 1777993 -94.78% gc-pause-total 2554 133 -94.79% latency-50 178448 170926 -4.22% latency-95 284350 198294 -30.26% latency-99 345191 220652 -36.08% rss 101564416 101007360 -0.55% sys-gc 6606832 6541296 -0.99% sys-heap 88801280 87752704 -1.18% sys-other 7334208 7405928 +0.98% sys-stack 524288 524288 +0.00% sys-total 103266608 102224216 -1.01% time 50339 46533 -7.56% virtual-mem 292990976 293728256 +0.25% garbage-1 allocated 2983818 2990889 +0.24% allocs 62880 62902 +0.03% cputime 16480000 16190000 -1.76% gc-pause-one 828462467 487875135 -41.11% gc-pause-total 4142312 2439375 -41.11% rss 1151709184 1153712128 +0.17% sys-gc 66068352 66068352 +0.00% sys-heap 1039728640 1039728640 +0.00% sys-other 37776064 40770176 +7.93% sys-stack 8781824 8781824 +0.00% sys-total 1152354880 1155348992 +0.26% time 16496998 16199876 -1.80% virtual-mem 1409564672 1402281984 -0.52% LGTM=rsc R=golang-codereviews, sameer, rsc, iant, jeremyjackins, gobot CC=golang-codereviews, khr https://golang.org/cl/46430043
2014-02-12 11:16:42 -07:00
res = false;
runtime: speedup GC sweep phase (batch free) benchmark old ns/op new ns/op delta garbage.BenchmarkParser 4370050250 3779668750 -13.51% garbage.BenchmarkParser-2 3713087000 3628771500 -2.27% garbage.BenchmarkParser-4 3519755250 3406349750 -3.22% garbage.BenchmarkParser-8 3386627750 3319144000 -1.99% garbage.BenchmarkTree 493585529 408102411 -17.32% garbage.BenchmarkTree-2 500487176 402285176 -19.62% garbage.BenchmarkTree-4 473238882 361484058 -23.61% garbage.BenchmarkTree-8 486977823 368334823 -24.36% garbage.BenchmarkTree2 31446600 31203200 -0.77% garbage.BenchmarkTree2-2 21469000 21077900 -1.82% garbage.BenchmarkTree2-4 11007600 10899100 -0.99% garbage.BenchmarkTree2-8 7692400 7032600 -8.58% garbage.BenchmarkParserPause 241863263 163249450 -32.50% garbage.BenchmarkParserPause-2 120135418 112981575 -5.95% garbage.BenchmarkParserPause-4 83411552 64580700 -22.58% garbage.BenchmarkParserPause-8 51870697 42207244 -18.63% garbage.BenchmarkTreePause 20940474 13147011 -37.22% garbage.BenchmarkTreePause-2 20115124 11146715 -44.59% garbage.BenchmarkTreePause-4 17217584 7486327 -56.52% garbage.BenchmarkTreePause-8 18258845 7400871 -59.47% garbage.BenchmarkTree2Pause 174067190 172674190 -0.80% garbage.BenchmarkTree2Pause-2 131175809 130615761 -0.43% garbage.BenchmarkTree2Pause-4 95406666 93972047 -1.50% garbage.BenchmarkTree2Pause-8 86056095 85334952 -0.84% garbage.BenchmarkParserLastPause 329932000 324790000 -1.56% garbage.BenchmarkParserLastPause-2 209383000 210456000 +0.51% garbage.BenchmarkParserLastPause-4 113981000 112921000 -0.93% garbage.BenchmarkParserLastPause-8 77967000 76625000 -1.72% garbage.BenchmarkTreeLastPause 29752000 18444000 -38.01% garbage.BenchmarkTreeLastPause-2 24274000 14766000 -39.17% garbage.BenchmarkTreeLastPause-4 19565000 8726000 -55.40% garbage.BenchmarkTreeLastPause-8 21956000 10530000 -52.04% garbage.BenchmarkTree2LastPause 314411000 311945000 -0.78% garbage.BenchmarkTree2LastPause-2 214641000 210836000 -1.77% garbage.BenchmarkTree2LastPause-4 110024000 108943000 -0.98% garbage.BenchmarkTree2LastPause-8 76873000 70263000 -8.60% R=golang-dev, rsc CC=golang-dev https://golang.org/cl/5991049
2012-04-12 02:01:24 -06:00
nfree = 0;
end = &head;
2014-06-26 09:54:39 -06:00
c = g->m->mcache;
runtime: concurrent GC sweep Moves sweep phase out of stoptheworld by adding background sweeper goroutine and lazy on-demand sweeping. It turned out to be somewhat trickier than I expected, because there is no point in time when we know size of live heap nor consistent number of mallocs and frees. So everything related to next_gc, mprof, memstats, etc becomes trickier. At the end of GC next_gc is conservatively set to heap_alloc*GOGC, which is much larger than real value. But after every sweep next_gc is decremented by freed*GOGC. So when everything is swept next_gc becomes what it should be. For mprof I had to introduce 3-generation scheme (allocs, revent_allocs, prev_allocs), because by the end of GC we know number of frees for the *previous* GC. Significant caution is required to not cross yet-unknown real value of next_gc. This is achieved by 2 means: 1. Whenever I allocate a span from MCentral, I sweep a span in that MCentral. 2. Whenever I allocate N pages from MHeap, I sweep until at least N pages are returned to heap. This provides quite strong guarantees that heap does not grow when it should now. http-1 allocated 7036 7033 -0.04% allocs 60 60 +0.00% cputime 51050 46700 -8.52% gc-pause-one 34060569 1777993 -94.78% gc-pause-total 2554 133 -94.79% latency-50 178448 170926 -4.22% latency-95 284350 198294 -30.26% latency-99 345191 220652 -36.08% rss 101564416 101007360 -0.55% sys-gc 6606832 6541296 -0.99% sys-heap 88801280 87752704 -1.18% sys-other 7334208 7405928 +0.98% sys-stack 524288 524288 +0.00% sys-total 103266608 102224216 -1.01% time 50339 46533 -7.56% virtual-mem 292990976 293728256 +0.25% garbage-1 allocated 2983818 2990889 +0.24% allocs 62880 62902 +0.03% cputime 16480000 16190000 -1.76% gc-pause-one 828462467 487875135 -41.11% gc-pause-total 4142312 2439375 -41.11% rss 1151709184 1153712128 +0.17% sys-gc 66068352 66068352 +0.00% sys-heap 1039728640 1039728640 +0.00% sys-other 37776064 40770176 +7.93% sys-stack 8781824 8781824 +0.00% sys-total 1152354880 1155348992 +0.26% time 16496998 16199876 -1.80% virtual-mem 1409564672 1402281984 -0.52% LGTM=rsc R=golang-codereviews, sameer, rsc, iant, jeremyjackins, gobot CC=golang-codereviews, khr https://golang.org/cl/46430043
2014-02-12 11:16:42 -07:00
sweepgenset = false;
// Mark any free objects in this span so we don't collect them.
for(link = s->freelist; link != nil; link = link->next) {
off = (uintptr*)link - (uintptr*)arena_start;
bitp = arena_start - off/wordsPerBitmapByte - 1;
shift = (off % wordsPerBitmapByte) * gcBits;
*bitp |= bitMarked<<shift;
}
// Unlink & free special records for any objects we're about to free.
specialp = &s->specials;
special = *specialp;
while(special != nil) {
// A finalizer can be set for an inner byte of an object, find object beginning.
p = (byte*)(s->start << PageShift) + special->offset/size*size;
off = (uintptr*)p - (uintptr*)arena_start;
bitp = arena_start - off/wordsPerBitmapByte - 1;
shift = (off % wordsPerBitmapByte) * gcBits;
bits = (*bitp>>shift) & bitMask;
if((bits&bitMarked) == 0) {
// Find the exact byte for which the special was setup
// (as opposed to object beginning).
p = (byte*)(s->start << PageShift) + special->offset;
// about to free object: splice out special record
y = special;
special = special->next;
*specialp = special;
runtime: concurrent GC sweep Moves sweep phase out of stoptheworld by adding background sweeper goroutine and lazy on-demand sweeping. It turned out to be somewhat trickier than I expected, because there is no point in time when we know size of live heap nor consistent number of mallocs and frees. So everything related to next_gc, mprof, memstats, etc becomes trickier. At the end of GC next_gc is conservatively set to heap_alloc*GOGC, which is much larger than real value. But after every sweep next_gc is decremented by freed*GOGC. So when everything is swept next_gc becomes what it should be. For mprof I had to introduce 3-generation scheme (allocs, revent_allocs, prev_allocs), because by the end of GC we know number of frees for the *previous* GC. Significant caution is required to not cross yet-unknown real value of next_gc. This is achieved by 2 means: 1. Whenever I allocate a span from MCentral, I sweep a span in that MCentral. 2. Whenever I allocate N pages from MHeap, I sweep until at least N pages are returned to heap. This provides quite strong guarantees that heap does not grow when it should now. http-1 allocated 7036 7033 -0.04% allocs 60 60 +0.00% cputime 51050 46700 -8.52% gc-pause-one 34060569 1777993 -94.78% gc-pause-total 2554 133 -94.79% latency-50 178448 170926 -4.22% latency-95 284350 198294 -30.26% latency-99 345191 220652 -36.08% rss 101564416 101007360 -0.55% sys-gc 6606832 6541296 -0.99% sys-heap 88801280 87752704 -1.18% sys-other 7334208 7405928 +0.98% sys-stack 524288 524288 +0.00% sys-total 103266608 102224216 -1.01% time 50339 46533 -7.56% virtual-mem 292990976 293728256 +0.25% garbage-1 allocated 2983818 2990889 +0.24% allocs 62880 62902 +0.03% cputime 16480000 16190000 -1.76% gc-pause-one 828462467 487875135 -41.11% gc-pause-total 4142312 2439375 -41.11% rss 1151709184 1153712128 +0.17% sys-gc 66068352 66068352 +0.00% sys-heap 1039728640 1039728640 +0.00% sys-other 37776064 40770176 +7.93% sys-stack 8781824 8781824 +0.00% sys-total 1152354880 1155348992 +0.26% time 16496998 16199876 -1.80% virtual-mem 1409564672 1402281984 -0.52% LGTM=rsc R=golang-codereviews, sameer, rsc, iant, jeremyjackins, gobot CC=golang-codereviews, khr https://golang.org/cl/46430043
2014-02-12 11:16:42 -07:00
if(!runtime·freespecial(y, p, size, false)) {
// stop freeing of object if it has a finalizer
*bitp |= bitMarked << shift;
}
} else {
// object is still live: keep special record
specialp = &special->next;
special = *specialp;
}
}
// Sweep through n objects of given size starting at p.
// This thread owns the span now, so it can manipulate
// the block bitmap without atomic operations.
p = (byte*)(s->start << PageShift);
// Find bits for the beginning of the span.
off = (uintptr*)p - (uintptr*)arena_start;
bitp = arena_start - off/wordsPerBitmapByte - 1;
shift = 0;
step = size/(PtrSize*wordsPerBitmapByte);
// Rewind to the previous quadruple as we move to the next
// in the beginning of the loop.
bitp += step;
if(step == 0) {
// 8-byte objects.
bitp++;
shift = gcBits;
}
for(; n > 0; n--, p += size) {
bitp -= step;
if(step == 0) {
if(shift != 0)
bitp--;
shift = gcBits - shift;
}
xbits = *bitp;
bits = (xbits>>shift) & bitMask;
// Allocated and marked object, reset bits to allocated.
if((bits&bitMarked) != 0) {
*bitp &= ~(bitMarked<<shift);
continue;
}
// At this point we know that we are looking at garbage object
// that needs to be collected.
runtime: adjust GODEBUG=allocfreetrace=1 and GODEBUG=gcdead=1 GODEBUG=allocfreetrace=1: The allocfreetrace=1 mode prints a stack trace for each block allocated and freed, and also a stack trace for each garbage collection. It was implemented by reusing the heap profiling support: if allocfreetrace=1 then the heap profile was effectively running at 1 sample per 1 byte allocated (always sample). The stack being shown at allocation was the stack gathered for profiling, meaning it was derived only from the program counters and did not include information about function arguments or frame pointers. The stack being shown at free was the allocation stack, not the free stack. If you are generating this log, you can find the allocation stack yourself, but it can be useful to see exactly the sequence that led to freeing the block: was it the garbage collector or an explicit free? Now that the garbage collector runs on an m0 stack, the stack trace for the garbage collector was never interesting. Fix all these problems: 1. Decouple allocfreetrace=1 from heap profiling. 2. Print the standard goroutine stack traces instead of a custom format. 3. Print the stack trace at time of allocation for an allocation, and print the stack trace at time of free (not the allocation trace again) for a free. 4. Print all goroutine stacks at garbage collection. Having all the stacks means that you can see the exact point at which each goroutine was preempted, which is often useful for identifying liveness-related errors. GODEBUG=gcdead=1: This mode overwrites dead pointers with a poison value. Detect the poison value as an invalid pointer during collection, the same way that small integers are invalid pointers. LGTM=khr R=khr CC=golang-codereviews https://golang.org/cl/81670043
2014-04-01 11:30:10 -06:00
if(runtime·debug.allocfreetrace)
runtime·tracefree(p, size);
// Reset to allocated+noscan.
*bitp = (xbits & ~((bitMarked|(BitsMask<<2))<<shift)) | ((uintptr)BitsDead<<(shift+2));
if(cl == 0) {
// Free large span.
if(preserve)
runtime·throw("can't preserve large span");
runtime·unmarkspan(p, s->npages<<PageShift);
s->needzero = 1;
runtime: concurrent GC sweep Moves sweep phase out of stoptheworld by adding background sweeper goroutine and lazy on-demand sweeping. It turned out to be somewhat trickier than I expected, because there is no point in time when we know size of live heap nor consistent number of mallocs and frees. So everything related to next_gc, mprof, memstats, etc becomes trickier. At the end of GC next_gc is conservatively set to heap_alloc*GOGC, which is much larger than real value. But after every sweep next_gc is decremented by freed*GOGC. So when everything is swept next_gc becomes what it should be. For mprof I had to introduce 3-generation scheme (allocs, revent_allocs, prev_allocs), because by the end of GC we know number of frees for the *previous* GC. Significant caution is required to not cross yet-unknown real value of next_gc. This is achieved by 2 means: 1. Whenever I allocate a span from MCentral, I sweep a span in that MCentral. 2. Whenever I allocate N pages from MHeap, I sweep until at least N pages are returned to heap. This provides quite strong guarantees that heap does not grow when it should now. http-1 allocated 7036 7033 -0.04% allocs 60 60 +0.00% cputime 51050 46700 -8.52% gc-pause-one 34060569 1777993 -94.78% gc-pause-total 2554 133 -94.79% latency-50 178448 170926 -4.22% latency-95 284350 198294 -30.26% latency-99 345191 220652 -36.08% rss 101564416 101007360 -0.55% sys-gc 6606832 6541296 -0.99% sys-heap 88801280 87752704 -1.18% sys-other 7334208 7405928 +0.98% sys-stack 524288 524288 +0.00% sys-total 103266608 102224216 -1.01% time 50339 46533 -7.56% virtual-mem 292990976 293728256 +0.25% garbage-1 allocated 2983818 2990889 +0.24% allocs 62880 62902 +0.03% cputime 16480000 16190000 -1.76% gc-pause-one 828462467 487875135 -41.11% gc-pause-total 4142312 2439375 -41.11% rss 1151709184 1153712128 +0.17% sys-gc 66068352 66068352 +0.00% sys-heap 1039728640 1039728640 +0.00% sys-other 37776064 40770176 +7.93% sys-stack 8781824 8781824 +0.00% sys-total 1152354880 1155348992 +0.26% time 16496998 16199876 -1.80% virtual-mem 1409564672 1402281984 -0.52% LGTM=rsc R=golang-codereviews, sameer, rsc, iant, jeremyjackins, gobot CC=golang-codereviews, khr https://golang.org/cl/46430043
2014-02-12 11:16:42 -07:00
// important to set sweepgen before returning it to heap
runtime·atomicstore(&s->sweepgen, sweepgen);
runtime: concurrent GC sweep Moves sweep phase out of stoptheworld by adding background sweeper goroutine and lazy on-demand sweeping. It turned out to be somewhat trickier than I expected, because there is no point in time when we know size of live heap nor consistent number of mallocs and frees. So everything related to next_gc, mprof, memstats, etc becomes trickier. At the end of GC next_gc is conservatively set to heap_alloc*GOGC, which is much larger than real value. But after every sweep next_gc is decremented by freed*GOGC. So when everything is swept next_gc becomes what it should be. For mprof I had to introduce 3-generation scheme (allocs, revent_allocs, prev_allocs), because by the end of GC we know number of frees for the *previous* GC. Significant caution is required to not cross yet-unknown real value of next_gc. This is achieved by 2 means: 1. Whenever I allocate a span from MCentral, I sweep a span in that MCentral. 2. Whenever I allocate N pages from MHeap, I sweep until at least N pages are returned to heap. This provides quite strong guarantees that heap does not grow when it should now. http-1 allocated 7036 7033 -0.04% allocs 60 60 +0.00% cputime 51050 46700 -8.52% gc-pause-one 34060569 1777993 -94.78% gc-pause-total 2554 133 -94.79% latency-50 178448 170926 -4.22% latency-95 284350 198294 -30.26% latency-99 345191 220652 -36.08% rss 101564416 101007360 -0.55% sys-gc 6606832 6541296 -0.99% sys-heap 88801280 87752704 -1.18% sys-other 7334208 7405928 +0.98% sys-stack 524288 524288 +0.00% sys-total 103266608 102224216 -1.01% time 50339 46533 -7.56% virtual-mem 292990976 293728256 +0.25% garbage-1 allocated 2983818 2990889 +0.24% allocs 62880 62902 +0.03% cputime 16480000 16190000 -1.76% gc-pause-one 828462467 487875135 -41.11% gc-pause-total 4142312 2439375 -41.11% rss 1151709184 1153712128 +0.17% sys-gc 66068352 66068352 +0.00% sys-heap 1039728640 1039728640 +0.00% sys-other 37776064 40770176 +7.93% sys-stack 8781824 8781824 +0.00% sys-total 1152354880 1155348992 +0.26% time 16496998 16199876 -1.80% virtual-mem 1409564672 1402281984 -0.52% LGTM=rsc R=golang-codereviews, sameer, rsc, iant, jeremyjackins, gobot CC=golang-codereviews, khr https://golang.org/cl/46430043
2014-02-12 11:16:42 -07:00
sweepgenset = true;
// NOTE(rsc,dvyukov): The original implementation of efence
// in CL 22060046 used SysFree instead of SysFault, so that
// the operating system would eventually give the memory
// back to us again, so that an efence program could run
// longer without running out of memory. Unfortunately,
// calling SysFree here without any kind of adjustment of the
// heap data structures means that when the memory does
// come back to us, we have the wrong metadata for it, either in
// the MSpan structures or in the garbage collection bitmap.
// Using SysFault here means that the program will run out of
// memory fairly quickly in efence mode, but at least it won't
// have mysterious crashes due to confused memory reuse.
// It should be possible to switch back to SysFree if we also
// implement and then call some kind of MHeap_DeleteSpan.
if(runtime·debug.efence) {
s->limit = nil; // prevent mlookup from finding this span
runtime·SysFault(p, size);
} else
runtime·MHeap_Free(&runtime·mheap, s, 1);
c->local_nlargefree++;
c->local_largefree += size;
runtime·xadd64(&mstats.next_gc, -(uint64)(size * (runtime·gcpercent + 100)/100));
runtime: concurrent GC sweep Moves sweep phase out of stoptheworld by adding background sweeper goroutine and lazy on-demand sweeping. It turned out to be somewhat trickier than I expected, because there is no point in time when we know size of live heap nor consistent number of mallocs and frees. So everything related to next_gc, mprof, memstats, etc becomes trickier. At the end of GC next_gc is conservatively set to heap_alloc*GOGC, which is much larger than real value. But after every sweep next_gc is decremented by freed*GOGC. So when everything is swept next_gc becomes what it should be. For mprof I had to introduce 3-generation scheme (allocs, revent_allocs, prev_allocs), because by the end of GC we know number of frees for the *previous* GC. Significant caution is required to not cross yet-unknown real value of next_gc. This is achieved by 2 means: 1. Whenever I allocate a span from MCentral, I sweep a span in that MCentral. 2. Whenever I allocate N pages from MHeap, I sweep until at least N pages are returned to heap. This provides quite strong guarantees that heap does not grow when it should now. http-1 allocated 7036 7033 -0.04% allocs 60 60 +0.00% cputime 51050 46700 -8.52% gc-pause-one 34060569 1777993 -94.78% gc-pause-total 2554 133 -94.79% latency-50 178448 170926 -4.22% latency-95 284350 198294 -30.26% latency-99 345191 220652 -36.08% rss 101564416 101007360 -0.55% sys-gc 6606832 6541296 -0.99% sys-heap 88801280 87752704 -1.18% sys-other 7334208 7405928 +0.98% sys-stack 524288 524288 +0.00% sys-total 103266608 102224216 -1.01% time 50339 46533 -7.56% virtual-mem 292990976 293728256 +0.25% garbage-1 allocated 2983818 2990889 +0.24% allocs 62880 62902 +0.03% cputime 16480000 16190000 -1.76% gc-pause-one 828462467 487875135 -41.11% gc-pause-total 4142312 2439375 -41.11% rss 1151709184 1153712128 +0.17% sys-gc 66068352 66068352 +0.00% sys-heap 1039728640 1039728640 +0.00% sys-other 37776064 40770176 +7.93% sys-stack 8781824 8781824 +0.00% sys-total 1152354880 1155348992 +0.26% time 16496998 16199876 -1.80% virtual-mem 1409564672 1402281984 -0.52% LGTM=rsc R=golang-codereviews, sameer, rsc, iant, jeremyjackins, gobot CC=golang-codereviews, khr https://golang.org/cl/46430043
2014-02-12 11:16:42 -07:00
res = true;
} else {
// Free small object.
if(size > 2*sizeof(uintptr))
((uintptr*)p)[1] = (uintptr)0xdeaddeaddeaddeadll; // mark as "needs to be zeroed"
else if(size > sizeof(uintptr))
((uintptr*)p)[1] = 0;
end->next = (MLink*)p;
runtime: speedup GC sweep phase (batch free) benchmark old ns/op new ns/op delta garbage.BenchmarkParser 4370050250 3779668750 -13.51% garbage.BenchmarkParser-2 3713087000 3628771500 -2.27% garbage.BenchmarkParser-4 3519755250 3406349750 -3.22% garbage.BenchmarkParser-8 3386627750 3319144000 -1.99% garbage.BenchmarkTree 493585529 408102411 -17.32% garbage.BenchmarkTree-2 500487176 402285176 -19.62% garbage.BenchmarkTree-4 473238882 361484058 -23.61% garbage.BenchmarkTree-8 486977823 368334823 -24.36% garbage.BenchmarkTree2 31446600 31203200 -0.77% garbage.BenchmarkTree2-2 21469000 21077900 -1.82% garbage.BenchmarkTree2-4 11007600 10899100 -0.99% garbage.BenchmarkTree2-8 7692400 7032600 -8.58% garbage.BenchmarkParserPause 241863263 163249450 -32.50% garbage.BenchmarkParserPause-2 120135418 112981575 -5.95% garbage.BenchmarkParserPause-4 83411552 64580700 -22.58% garbage.BenchmarkParserPause-8 51870697 42207244 -18.63% garbage.BenchmarkTreePause 20940474 13147011 -37.22% garbage.BenchmarkTreePause-2 20115124 11146715 -44.59% garbage.BenchmarkTreePause-4 17217584 7486327 -56.52% garbage.BenchmarkTreePause-8 18258845 7400871 -59.47% garbage.BenchmarkTree2Pause 174067190 172674190 -0.80% garbage.BenchmarkTree2Pause-2 131175809 130615761 -0.43% garbage.BenchmarkTree2Pause-4 95406666 93972047 -1.50% garbage.BenchmarkTree2Pause-8 86056095 85334952 -0.84% garbage.BenchmarkParserLastPause 329932000 324790000 -1.56% garbage.BenchmarkParserLastPause-2 209383000 210456000 +0.51% garbage.BenchmarkParserLastPause-4 113981000 112921000 -0.93% garbage.BenchmarkParserLastPause-8 77967000 76625000 -1.72% garbage.BenchmarkTreeLastPause 29752000 18444000 -38.01% garbage.BenchmarkTreeLastPause-2 24274000 14766000 -39.17% garbage.BenchmarkTreeLastPause-4 19565000 8726000 -55.40% garbage.BenchmarkTreeLastPause-8 21956000 10530000 -52.04% garbage.BenchmarkTree2LastPause 314411000 311945000 -0.78% garbage.BenchmarkTree2LastPause-2 214641000 210836000 -1.77% garbage.BenchmarkTree2LastPause-4 110024000 108943000 -0.98% garbage.BenchmarkTree2LastPause-8 76873000 70263000 -8.60% R=golang-dev, rsc CC=golang-dev https://golang.org/cl/5991049
2012-04-12 02:01:24 -06:00
end = (MLink*)p;
nfree++;
}
runtime: speedup GC sweep phase (batch free) benchmark old ns/op new ns/op delta garbage.BenchmarkParser 4370050250 3779668750 -13.51% garbage.BenchmarkParser-2 3713087000 3628771500 -2.27% garbage.BenchmarkParser-4 3519755250 3406349750 -3.22% garbage.BenchmarkParser-8 3386627750 3319144000 -1.99% garbage.BenchmarkTree 493585529 408102411 -17.32% garbage.BenchmarkTree-2 500487176 402285176 -19.62% garbage.BenchmarkTree-4 473238882 361484058 -23.61% garbage.BenchmarkTree-8 486977823 368334823 -24.36% garbage.BenchmarkTree2 31446600 31203200 -0.77% garbage.BenchmarkTree2-2 21469000 21077900 -1.82% garbage.BenchmarkTree2-4 11007600 10899100 -0.99% garbage.BenchmarkTree2-8 7692400 7032600 -8.58% garbage.BenchmarkParserPause 241863263 163249450 -32.50% garbage.BenchmarkParserPause-2 120135418 112981575 -5.95% garbage.BenchmarkParserPause-4 83411552 64580700 -22.58% garbage.BenchmarkParserPause-8 51870697 42207244 -18.63% garbage.BenchmarkTreePause 20940474 13147011 -37.22% garbage.BenchmarkTreePause-2 20115124 11146715 -44.59% garbage.BenchmarkTreePause-4 17217584 7486327 -56.52% garbage.BenchmarkTreePause-8 18258845 7400871 -59.47% garbage.BenchmarkTree2Pause 174067190 172674190 -0.80% garbage.BenchmarkTree2Pause-2 131175809 130615761 -0.43% garbage.BenchmarkTree2Pause-4 95406666 93972047 -1.50% garbage.BenchmarkTree2Pause-8 86056095 85334952 -0.84% garbage.BenchmarkParserLastPause 329932000 324790000 -1.56% garbage.BenchmarkParserLastPause-2 209383000 210456000 +0.51% garbage.BenchmarkParserLastPause-4 113981000 112921000 -0.93% garbage.BenchmarkParserLastPause-8 77967000 76625000 -1.72% garbage.BenchmarkTreeLastPause 29752000 18444000 -38.01% garbage.BenchmarkTreeLastPause-2 24274000 14766000 -39.17% garbage.BenchmarkTreeLastPause-4 19565000 8726000 -55.40% garbage.BenchmarkTreeLastPause-8 21956000 10530000 -52.04% garbage.BenchmarkTree2LastPause 314411000 311945000 -0.78% garbage.BenchmarkTree2LastPause-2 214641000 210836000 -1.77% garbage.BenchmarkTree2LastPause-4 110024000 108943000 -0.98% garbage.BenchmarkTree2LastPause-8 76873000 70263000 -8.60% R=golang-dev, rsc CC=golang-dev https://golang.org/cl/5991049
2012-04-12 02:01:24 -06:00
}
// We need to set s->sweepgen = h->sweepgen only when all blocks are swept,
// because of the potential for a concurrent free/SetFinalizer.
// But we need to set it before we make the span available for allocation
// (return it to heap or mcentral), because allocation code assumes that a
// span is already swept if available for allocation.
if(!sweepgenset && nfree == 0) {
// The span must be in our exclusive ownership until we update sweepgen,
// check for potential races.
if(s->state != MSpanInUse || s->sweepgen != sweepgen-1) {
runtime·printf("MSpan_Sweep: state=%d sweepgen=%d mheap.sweepgen=%d\n",
s->state, s->sweepgen, sweepgen);
runtime·throw("MSpan_Sweep: bad span state after sweep");
}
runtime·atomicstore(&s->sweepgen, sweepgen);
}
if(nfree > 0) {
c->local_nsmallfree[cl] += nfree;
runtime: speedup GC sweep phase (batch free) benchmark old ns/op new ns/op delta garbage.BenchmarkParser 4370050250 3779668750 -13.51% garbage.BenchmarkParser-2 3713087000 3628771500 -2.27% garbage.BenchmarkParser-4 3519755250 3406349750 -3.22% garbage.BenchmarkParser-8 3386627750 3319144000 -1.99% garbage.BenchmarkTree 493585529 408102411 -17.32% garbage.BenchmarkTree-2 500487176 402285176 -19.62% garbage.BenchmarkTree-4 473238882 361484058 -23.61% garbage.BenchmarkTree-8 486977823 368334823 -24.36% garbage.BenchmarkTree2 31446600 31203200 -0.77% garbage.BenchmarkTree2-2 21469000 21077900 -1.82% garbage.BenchmarkTree2-4 11007600 10899100 -0.99% garbage.BenchmarkTree2-8 7692400 7032600 -8.58% garbage.BenchmarkParserPause 241863263 163249450 -32.50% garbage.BenchmarkParserPause-2 120135418 112981575 -5.95% garbage.BenchmarkParserPause-4 83411552 64580700 -22.58% garbage.BenchmarkParserPause-8 51870697 42207244 -18.63% garbage.BenchmarkTreePause 20940474 13147011 -37.22% garbage.BenchmarkTreePause-2 20115124 11146715 -44.59% garbage.BenchmarkTreePause-4 17217584 7486327 -56.52% garbage.BenchmarkTreePause-8 18258845 7400871 -59.47% garbage.BenchmarkTree2Pause 174067190 172674190 -0.80% garbage.BenchmarkTree2Pause-2 131175809 130615761 -0.43% garbage.BenchmarkTree2Pause-4 95406666 93972047 -1.50% garbage.BenchmarkTree2Pause-8 86056095 85334952 -0.84% garbage.BenchmarkParserLastPause 329932000 324790000 -1.56% garbage.BenchmarkParserLastPause-2 209383000 210456000 +0.51% garbage.BenchmarkParserLastPause-4 113981000 112921000 -0.93% garbage.BenchmarkParserLastPause-8 77967000 76625000 -1.72% garbage.BenchmarkTreeLastPause 29752000 18444000 -38.01% garbage.BenchmarkTreeLastPause-2 24274000 14766000 -39.17% garbage.BenchmarkTreeLastPause-4 19565000 8726000 -55.40% garbage.BenchmarkTreeLastPause-8 21956000 10530000 -52.04% garbage.BenchmarkTree2LastPause 314411000 311945000 -0.78% garbage.BenchmarkTree2LastPause-2 214641000 210836000 -1.77% garbage.BenchmarkTree2LastPause-4 110024000 108943000 -0.98% garbage.BenchmarkTree2LastPause-8 76873000 70263000 -8.60% R=golang-dev, rsc CC=golang-dev https://golang.org/cl/5991049
2012-04-12 02:01:24 -06:00
c->local_cachealloc -= nfree * size;
runtime·xadd64(&mstats.next_gc, -(uint64)(nfree * size * (runtime·gcpercent + 100)/100));
res = runtime·MCentral_FreeSpan(&runtime·mheap.central[cl].mcentral, s, nfree, head.next, end, preserve);
// MCentral_FreeSpan updates sweepgen
runtime: concurrent GC sweep Moves sweep phase out of stoptheworld by adding background sweeper goroutine and lazy on-demand sweeping. It turned out to be somewhat trickier than I expected, because there is no point in time when we know size of live heap nor consistent number of mallocs and frees. So everything related to next_gc, mprof, memstats, etc becomes trickier. At the end of GC next_gc is conservatively set to heap_alloc*GOGC, which is much larger than real value. But after every sweep next_gc is decremented by freed*GOGC. So when everything is swept next_gc becomes what it should be. For mprof I had to introduce 3-generation scheme (allocs, revent_allocs, prev_allocs), because by the end of GC we know number of frees for the *previous* GC. Significant caution is required to not cross yet-unknown real value of next_gc. This is achieved by 2 means: 1. Whenever I allocate a span from MCentral, I sweep a span in that MCentral. 2. Whenever I allocate N pages from MHeap, I sweep until at least N pages are returned to heap. This provides quite strong guarantees that heap does not grow when it should now. http-1 allocated 7036 7033 -0.04% allocs 60 60 +0.00% cputime 51050 46700 -8.52% gc-pause-one 34060569 1777993 -94.78% gc-pause-total 2554 133 -94.79% latency-50 178448 170926 -4.22% latency-95 284350 198294 -30.26% latency-99 345191 220652 -36.08% rss 101564416 101007360 -0.55% sys-gc 6606832 6541296 -0.99% sys-heap 88801280 87752704 -1.18% sys-other 7334208 7405928 +0.98% sys-stack 524288 524288 +0.00% sys-total 103266608 102224216 -1.01% time 50339 46533 -7.56% virtual-mem 292990976 293728256 +0.25% garbage-1 allocated 2983818 2990889 +0.24% allocs 62880 62902 +0.03% cputime 16480000 16190000 -1.76% gc-pause-one 828462467 487875135 -41.11% gc-pause-total 4142312 2439375 -41.11% rss 1151709184 1153712128 +0.17% sys-gc 66068352 66068352 +0.00% sys-heap 1039728640 1039728640 +0.00% sys-other 37776064 40770176 +7.93% sys-stack 8781824 8781824 +0.00% sys-total 1152354880 1155348992 +0.26% time 16496998 16199876 -1.80% virtual-mem 1409564672 1402281984 -0.52% LGTM=rsc R=golang-codereviews, sameer, rsc, iant, jeremyjackins, gobot CC=golang-codereviews, khr https://golang.org/cl/46430043
2014-02-12 11:16:42 -07:00
}
return res;
}
// State of background sweep.
// Pretected by gclock.
static struct
{
G* g;
bool parked;
MSpan** spans;
uint32 nspan;
uint32 spanidx;
uint32 nbgsweep;
uint32 npausesweep;
runtime: concurrent GC sweep Moves sweep phase out of stoptheworld by adding background sweeper goroutine and lazy on-demand sweeping. It turned out to be somewhat trickier than I expected, because there is no point in time when we know size of live heap nor consistent number of mallocs and frees. So everything related to next_gc, mprof, memstats, etc becomes trickier. At the end of GC next_gc is conservatively set to heap_alloc*GOGC, which is much larger than real value. But after every sweep next_gc is decremented by freed*GOGC. So when everything is swept next_gc becomes what it should be. For mprof I had to introduce 3-generation scheme (allocs, revent_allocs, prev_allocs), because by the end of GC we know number of frees for the *previous* GC. Significant caution is required to not cross yet-unknown real value of next_gc. This is achieved by 2 means: 1. Whenever I allocate a span from MCentral, I sweep a span in that MCentral. 2. Whenever I allocate N pages from MHeap, I sweep until at least N pages are returned to heap. This provides quite strong guarantees that heap does not grow when it should now. http-1 allocated 7036 7033 -0.04% allocs 60 60 +0.00% cputime 51050 46700 -8.52% gc-pause-one 34060569 1777993 -94.78% gc-pause-total 2554 133 -94.79% latency-50 178448 170926 -4.22% latency-95 284350 198294 -30.26% latency-99 345191 220652 -36.08% rss 101564416 101007360 -0.55% sys-gc 6606832 6541296 -0.99% sys-heap 88801280 87752704 -1.18% sys-other 7334208 7405928 +0.98% sys-stack 524288 524288 +0.00% sys-total 103266608 102224216 -1.01% time 50339 46533 -7.56% virtual-mem 292990976 293728256 +0.25% garbage-1 allocated 2983818 2990889 +0.24% allocs 62880 62902 +0.03% cputime 16480000 16190000 -1.76% gc-pause-one 828462467 487875135 -41.11% gc-pause-total 4142312 2439375 -41.11% rss 1151709184 1153712128 +0.17% sys-gc 66068352 66068352 +0.00% sys-heap 1039728640 1039728640 +0.00% sys-other 37776064 40770176 +7.93% sys-stack 8781824 8781824 +0.00% sys-total 1152354880 1155348992 +0.26% time 16496998 16199876 -1.80% virtual-mem 1409564672 1402281984 -0.52% LGTM=rsc R=golang-codereviews, sameer, rsc, iant, jeremyjackins, gobot CC=golang-codereviews, khr https://golang.org/cl/46430043
2014-02-12 11:16:42 -07:00
} sweep;
// background sweeping goroutine
static void
bgsweep(void)
{
g->issystem = 1;
for(;;) {
while(runtime·sweepone() != -1) {
sweep.nbgsweep++;
runtime: concurrent GC sweep Moves sweep phase out of stoptheworld by adding background sweeper goroutine and lazy on-demand sweeping. It turned out to be somewhat trickier than I expected, because there is no point in time when we know size of live heap nor consistent number of mallocs and frees. So everything related to next_gc, mprof, memstats, etc becomes trickier. At the end of GC next_gc is conservatively set to heap_alloc*GOGC, which is much larger than real value. But after every sweep next_gc is decremented by freed*GOGC. So when everything is swept next_gc becomes what it should be. For mprof I had to introduce 3-generation scheme (allocs, revent_allocs, prev_allocs), because by the end of GC we know number of frees for the *previous* GC. Significant caution is required to not cross yet-unknown real value of next_gc. This is achieved by 2 means: 1. Whenever I allocate a span from MCentral, I sweep a span in that MCentral. 2. Whenever I allocate N pages from MHeap, I sweep until at least N pages are returned to heap. This provides quite strong guarantees that heap does not grow when it should now. http-1 allocated 7036 7033 -0.04% allocs 60 60 +0.00% cputime 51050 46700 -8.52% gc-pause-one 34060569 1777993 -94.78% gc-pause-total 2554 133 -94.79% latency-50 178448 170926 -4.22% latency-95 284350 198294 -30.26% latency-99 345191 220652 -36.08% rss 101564416 101007360 -0.55% sys-gc 6606832 6541296 -0.99% sys-heap 88801280 87752704 -1.18% sys-other 7334208 7405928 +0.98% sys-stack 524288 524288 +0.00% sys-total 103266608 102224216 -1.01% time 50339 46533 -7.56% virtual-mem 292990976 293728256 +0.25% garbage-1 allocated 2983818 2990889 +0.24% allocs 62880 62902 +0.03% cputime 16480000 16190000 -1.76% gc-pause-one 828462467 487875135 -41.11% gc-pause-total 4142312 2439375 -41.11% rss 1151709184 1153712128 +0.17% sys-gc 66068352 66068352 +0.00% sys-heap 1039728640 1039728640 +0.00% sys-other 37776064 40770176 +7.93% sys-stack 8781824 8781824 +0.00% sys-total 1152354880 1155348992 +0.26% time 16496998 16199876 -1.80% virtual-mem 1409564672 1402281984 -0.52% LGTM=rsc R=golang-codereviews, sameer, rsc, iant, jeremyjackins, gobot CC=golang-codereviews, khr https://golang.org/cl/46430043
2014-02-12 11:16:42 -07:00
runtime·gosched();
}
runtime·lock(&gclock);
if(!runtime·mheap.sweepdone) {
// It's possible if GC has happened between sweepone has
// returned -1 and gclock lock.
runtime·unlock(&gclock);
continue;
}
runtime: concurrent GC sweep Moves sweep phase out of stoptheworld by adding background sweeper goroutine and lazy on-demand sweeping. It turned out to be somewhat trickier than I expected, because there is no point in time when we know size of live heap nor consistent number of mallocs and frees. So everything related to next_gc, mprof, memstats, etc becomes trickier. At the end of GC next_gc is conservatively set to heap_alloc*GOGC, which is much larger than real value. But after every sweep next_gc is decremented by freed*GOGC. So when everything is swept next_gc becomes what it should be. For mprof I had to introduce 3-generation scheme (allocs, revent_allocs, prev_allocs), because by the end of GC we know number of frees for the *previous* GC. Significant caution is required to not cross yet-unknown real value of next_gc. This is achieved by 2 means: 1. Whenever I allocate a span from MCentral, I sweep a span in that MCentral. 2. Whenever I allocate N pages from MHeap, I sweep until at least N pages are returned to heap. This provides quite strong guarantees that heap does not grow when it should now. http-1 allocated 7036 7033 -0.04% allocs 60 60 +0.00% cputime 51050 46700 -8.52% gc-pause-one 34060569 1777993 -94.78% gc-pause-total 2554 133 -94.79% latency-50 178448 170926 -4.22% latency-95 284350 198294 -30.26% latency-99 345191 220652 -36.08% rss 101564416 101007360 -0.55% sys-gc 6606832 6541296 -0.99% sys-heap 88801280 87752704 -1.18% sys-other 7334208 7405928 +0.98% sys-stack 524288 524288 +0.00% sys-total 103266608 102224216 -1.01% time 50339 46533 -7.56% virtual-mem 292990976 293728256 +0.25% garbage-1 allocated 2983818 2990889 +0.24% allocs 62880 62902 +0.03% cputime 16480000 16190000 -1.76% gc-pause-one 828462467 487875135 -41.11% gc-pause-total 4142312 2439375 -41.11% rss 1151709184 1153712128 +0.17% sys-gc 66068352 66068352 +0.00% sys-heap 1039728640 1039728640 +0.00% sys-other 37776064 40770176 +7.93% sys-stack 8781824 8781824 +0.00% sys-total 1152354880 1155348992 +0.26% time 16496998 16199876 -1.80% virtual-mem 1409564672 1402281984 -0.52% LGTM=rsc R=golang-codereviews, sameer, rsc, iant, jeremyjackins, gobot CC=golang-codereviews, khr https://golang.org/cl/46430043
2014-02-12 11:16:42 -07:00
sweep.parked = true;
g->isbackground = true;
runtime: concurrent GC sweep Moves sweep phase out of stoptheworld by adding background sweeper goroutine and lazy on-demand sweeping. It turned out to be somewhat trickier than I expected, because there is no point in time when we know size of live heap nor consistent number of mallocs and frees. So everything related to next_gc, mprof, memstats, etc becomes trickier. At the end of GC next_gc is conservatively set to heap_alloc*GOGC, which is much larger than real value. But after every sweep next_gc is decremented by freed*GOGC. So when everything is swept next_gc becomes what it should be. For mprof I had to introduce 3-generation scheme (allocs, revent_allocs, prev_allocs), because by the end of GC we know number of frees for the *previous* GC. Significant caution is required to not cross yet-unknown real value of next_gc. This is achieved by 2 means: 1. Whenever I allocate a span from MCentral, I sweep a span in that MCentral. 2. Whenever I allocate N pages from MHeap, I sweep until at least N pages are returned to heap. This provides quite strong guarantees that heap does not grow when it should now. http-1 allocated 7036 7033 -0.04% allocs 60 60 +0.00% cputime 51050 46700 -8.52% gc-pause-one 34060569 1777993 -94.78% gc-pause-total 2554 133 -94.79% latency-50 178448 170926 -4.22% latency-95 284350 198294 -30.26% latency-99 345191 220652 -36.08% rss 101564416 101007360 -0.55% sys-gc 6606832 6541296 -0.99% sys-heap 88801280 87752704 -1.18% sys-other 7334208 7405928 +0.98% sys-stack 524288 524288 +0.00% sys-total 103266608 102224216 -1.01% time 50339 46533 -7.56% virtual-mem 292990976 293728256 +0.25% garbage-1 allocated 2983818 2990889 +0.24% allocs 62880 62902 +0.03% cputime 16480000 16190000 -1.76% gc-pause-one 828462467 487875135 -41.11% gc-pause-total 4142312 2439375 -41.11% rss 1151709184 1153712128 +0.17% sys-gc 66068352 66068352 +0.00% sys-heap 1039728640 1039728640 +0.00% sys-other 37776064 40770176 +7.93% sys-stack 8781824 8781824 +0.00% sys-total 1152354880 1155348992 +0.26% time 16496998 16199876 -1.80% virtual-mem 1409564672 1402281984 -0.52% LGTM=rsc R=golang-codereviews, sameer, rsc, iant, jeremyjackins, gobot CC=golang-codereviews, khr https://golang.org/cl/46430043
2014-02-12 11:16:42 -07:00
runtime·parkunlock(&gclock, "GC sweep wait");
g->isbackground = false;
runtime: concurrent GC sweep Moves sweep phase out of stoptheworld by adding background sweeper goroutine and lazy on-demand sweeping. It turned out to be somewhat trickier than I expected, because there is no point in time when we know size of live heap nor consistent number of mallocs and frees. So everything related to next_gc, mprof, memstats, etc becomes trickier. At the end of GC next_gc is conservatively set to heap_alloc*GOGC, which is much larger than real value. But after every sweep next_gc is decremented by freed*GOGC. So when everything is swept next_gc becomes what it should be. For mprof I had to introduce 3-generation scheme (allocs, revent_allocs, prev_allocs), because by the end of GC we know number of frees for the *previous* GC. Significant caution is required to not cross yet-unknown real value of next_gc. This is achieved by 2 means: 1. Whenever I allocate a span from MCentral, I sweep a span in that MCentral. 2. Whenever I allocate N pages from MHeap, I sweep until at least N pages are returned to heap. This provides quite strong guarantees that heap does not grow when it should now. http-1 allocated 7036 7033 -0.04% allocs 60 60 +0.00% cputime 51050 46700 -8.52% gc-pause-one 34060569 1777993 -94.78% gc-pause-total 2554 133 -94.79% latency-50 178448 170926 -4.22% latency-95 284350 198294 -30.26% latency-99 345191 220652 -36.08% rss 101564416 101007360 -0.55% sys-gc 6606832 6541296 -0.99% sys-heap 88801280 87752704 -1.18% sys-other 7334208 7405928 +0.98% sys-stack 524288 524288 +0.00% sys-total 103266608 102224216 -1.01% time 50339 46533 -7.56% virtual-mem 292990976 293728256 +0.25% garbage-1 allocated 2983818 2990889 +0.24% allocs 62880 62902 +0.03% cputime 16480000 16190000 -1.76% gc-pause-one 828462467 487875135 -41.11% gc-pause-total 4142312 2439375 -41.11% rss 1151709184 1153712128 +0.17% sys-gc 66068352 66068352 +0.00% sys-heap 1039728640 1039728640 +0.00% sys-other 37776064 40770176 +7.93% sys-stack 8781824 8781824 +0.00% sys-total 1152354880 1155348992 +0.26% time 16496998 16199876 -1.80% virtual-mem 1409564672 1402281984 -0.52% LGTM=rsc R=golang-codereviews, sameer, rsc, iant, jeremyjackins, gobot CC=golang-codereviews, khr https://golang.org/cl/46430043
2014-02-12 11:16:42 -07:00
}
}
// sweeps one span
// returns number of pages returned to heap, or -1 if there is nothing to sweep
uintptr
runtime·sweepone(void)
{
MSpan *s;
uint32 idx, sg;
uintptr npages;
// increment locks to ensure that the goroutine is not preempted
// in the middle of sweep thus leaving the span in an inconsistent state for next GC
2014-06-26 09:54:39 -06:00
g->m->locks++;
runtime: concurrent GC sweep Moves sweep phase out of stoptheworld by adding background sweeper goroutine and lazy on-demand sweeping. It turned out to be somewhat trickier than I expected, because there is no point in time when we know size of live heap nor consistent number of mallocs and frees. So everything related to next_gc, mprof, memstats, etc becomes trickier. At the end of GC next_gc is conservatively set to heap_alloc*GOGC, which is much larger than real value. But after every sweep next_gc is decremented by freed*GOGC. So when everything is swept next_gc becomes what it should be. For mprof I had to introduce 3-generation scheme (allocs, revent_allocs, prev_allocs), because by the end of GC we know number of frees for the *previous* GC. Significant caution is required to not cross yet-unknown real value of next_gc. This is achieved by 2 means: 1. Whenever I allocate a span from MCentral, I sweep a span in that MCentral. 2. Whenever I allocate N pages from MHeap, I sweep until at least N pages are returned to heap. This provides quite strong guarantees that heap does not grow when it should now. http-1 allocated 7036 7033 -0.04% allocs 60 60 +0.00% cputime 51050 46700 -8.52% gc-pause-one 34060569 1777993 -94.78% gc-pause-total 2554 133 -94.79% latency-50 178448 170926 -4.22% latency-95 284350 198294 -30.26% latency-99 345191 220652 -36.08% rss 101564416 101007360 -0.55% sys-gc 6606832 6541296 -0.99% sys-heap 88801280 87752704 -1.18% sys-other 7334208 7405928 +0.98% sys-stack 524288 524288 +0.00% sys-total 103266608 102224216 -1.01% time 50339 46533 -7.56% virtual-mem 292990976 293728256 +0.25% garbage-1 allocated 2983818 2990889 +0.24% allocs 62880 62902 +0.03% cputime 16480000 16190000 -1.76% gc-pause-one 828462467 487875135 -41.11% gc-pause-total 4142312 2439375 -41.11% rss 1151709184 1153712128 +0.17% sys-gc 66068352 66068352 +0.00% sys-heap 1039728640 1039728640 +0.00% sys-other 37776064 40770176 +7.93% sys-stack 8781824 8781824 +0.00% sys-total 1152354880 1155348992 +0.26% time 16496998 16199876 -1.80% virtual-mem 1409564672 1402281984 -0.52% LGTM=rsc R=golang-codereviews, sameer, rsc, iant, jeremyjackins, gobot CC=golang-codereviews, khr https://golang.org/cl/46430043
2014-02-12 11:16:42 -07:00
sg = runtime·mheap.sweepgen;
for(;;) {
idx = runtime·xadd(&sweep.spanidx, 1) - 1;
if(idx >= sweep.nspan) {
runtime·mheap.sweepdone = true;
2014-06-26 09:54:39 -06:00
g->m->locks--;
runtime: concurrent GC sweep Moves sweep phase out of stoptheworld by adding background sweeper goroutine and lazy on-demand sweeping. It turned out to be somewhat trickier than I expected, because there is no point in time when we know size of live heap nor consistent number of mallocs and frees. So everything related to next_gc, mprof, memstats, etc becomes trickier. At the end of GC next_gc is conservatively set to heap_alloc*GOGC, which is much larger than real value. But after every sweep next_gc is decremented by freed*GOGC. So when everything is swept next_gc becomes what it should be. For mprof I had to introduce 3-generation scheme (allocs, revent_allocs, prev_allocs), because by the end of GC we know number of frees for the *previous* GC. Significant caution is required to not cross yet-unknown real value of next_gc. This is achieved by 2 means: 1. Whenever I allocate a span from MCentral, I sweep a span in that MCentral. 2. Whenever I allocate N pages from MHeap, I sweep until at least N pages are returned to heap. This provides quite strong guarantees that heap does not grow when it should now. http-1 allocated 7036 7033 -0.04% allocs 60 60 +0.00% cputime 51050 46700 -8.52% gc-pause-one 34060569 1777993 -94.78% gc-pause-total 2554 133 -94.79% latency-50 178448 170926 -4.22% latency-95 284350 198294 -30.26% latency-99 345191 220652 -36.08% rss 101564416 101007360 -0.55% sys-gc 6606832 6541296 -0.99% sys-heap 88801280 87752704 -1.18% sys-other 7334208 7405928 +0.98% sys-stack 524288 524288 +0.00% sys-total 103266608 102224216 -1.01% time 50339 46533 -7.56% virtual-mem 292990976 293728256 +0.25% garbage-1 allocated 2983818 2990889 +0.24% allocs 62880 62902 +0.03% cputime 16480000 16190000 -1.76% gc-pause-one 828462467 487875135 -41.11% gc-pause-total 4142312 2439375 -41.11% rss 1151709184 1153712128 +0.17% sys-gc 66068352 66068352 +0.00% sys-heap 1039728640 1039728640 +0.00% sys-other 37776064 40770176 +7.93% sys-stack 8781824 8781824 +0.00% sys-total 1152354880 1155348992 +0.26% time 16496998 16199876 -1.80% virtual-mem 1409564672 1402281984 -0.52% LGTM=rsc R=golang-codereviews, sameer, rsc, iant, jeremyjackins, gobot CC=golang-codereviews, khr https://golang.org/cl/46430043
2014-02-12 11:16:42 -07:00
return -1;
}
s = sweep.spans[idx];
if(s->state != MSpanInUse) {
s->sweepgen = sg;
continue;
}
if(s->sweepgen != sg-2 || !runtime·cas(&s->sweepgen, sg-2, sg-1))
continue;
npages = s->npages;
if(!runtime·MSpan_Sweep(s, false))
runtime: concurrent GC sweep Moves sweep phase out of stoptheworld by adding background sweeper goroutine and lazy on-demand sweeping. It turned out to be somewhat trickier than I expected, because there is no point in time when we know size of live heap nor consistent number of mallocs and frees. So everything related to next_gc, mprof, memstats, etc becomes trickier. At the end of GC next_gc is conservatively set to heap_alloc*GOGC, which is much larger than real value. But after every sweep next_gc is decremented by freed*GOGC. So when everything is swept next_gc becomes what it should be. For mprof I had to introduce 3-generation scheme (allocs, revent_allocs, prev_allocs), because by the end of GC we know number of frees for the *previous* GC. Significant caution is required to not cross yet-unknown real value of next_gc. This is achieved by 2 means: 1. Whenever I allocate a span from MCentral, I sweep a span in that MCentral. 2. Whenever I allocate N pages from MHeap, I sweep until at least N pages are returned to heap. This provides quite strong guarantees that heap does not grow when it should now. http-1 allocated 7036 7033 -0.04% allocs 60 60 +0.00% cputime 51050 46700 -8.52% gc-pause-one 34060569 1777993 -94.78% gc-pause-total 2554 133 -94.79% latency-50 178448 170926 -4.22% latency-95 284350 198294 -30.26% latency-99 345191 220652 -36.08% rss 101564416 101007360 -0.55% sys-gc 6606832 6541296 -0.99% sys-heap 88801280 87752704 -1.18% sys-other 7334208 7405928 +0.98% sys-stack 524288 524288 +0.00% sys-total 103266608 102224216 -1.01% time 50339 46533 -7.56% virtual-mem 292990976 293728256 +0.25% garbage-1 allocated 2983818 2990889 +0.24% allocs 62880 62902 +0.03% cputime 16480000 16190000 -1.76% gc-pause-one 828462467 487875135 -41.11% gc-pause-total 4142312 2439375 -41.11% rss 1151709184 1153712128 +0.17% sys-gc 66068352 66068352 +0.00% sys-heap 1039728640 1039728640 +0.00% sys-other 37776064 40770176 +7.93% sys-stack 8781824 8781824 +0.00% sys-total 1152354880 1155348992 +0.26% time 16496998 16199876 -1.80% virtual-mem 1409564672 1402281984 -0.52% LGTM=rsc R=golang-codereviews, sameer, rsc, iant, jeremyjackins, gobot CC=golang-codereviews, khr https://golang.org/cl/46430043
2014-02-12 11:16:42 -07:00
npages = 0;
2014-06-26 09:54:39 -06:00
g->m->locks--;
runtime: concurrent GC sweep Moves sweep phase out of stoptheworld by adding background sweeper goroutine and lazy on-demand sweeping. It turned out to be somewhat trickier than I expected, because there is no point in time when we know size of live heap nor consistent number of mallocs and frees. So everything related to next_gc, mprof, memstats, etc becomes trickier. At the end of GC next_gc is conservatively set to heap_alloc*GOGC, which is much larger than real value. But after every sweep next_gc is decremented by freed*GOGC. So when everything is swept next_gc becomes what it should be. For mprof I had to introduce 3-generation scheme (allocs, revent_allocs, prev_allocs), because by the end of GC we know number of frees for the *previous* GC. Significant caution is required to not cross yet-unknown real value of next_gc. This is achieved by 2 means: 1. Whenever I allocate a span from MCentral, I sweep a span in that MCentral. 2. Whenever I allocate N pages from MHeap, I sweep until at least N pages are returned to heap. This provides quite strong guarantees that heap does not grow when it should now. http-1 allocated 7036 7033 -0.04% allocs 60 60 +0.00% cputime 51050 46700 -8.52% gc-pause-one 34060569 1777993 -94.78% gc-pause-total 2554 133 -94.79% latency-50 178448 170926 -4.22% latency-95 284350 198294 -30.26% latency-99 345191 220652 -36.08% rss 101564416 101007360 -0.55% sys-gc 6606832 6541296 -0.99% sys-heap 88801280 87752704 -1.18% sys-other 7334208 7405928 +0.98% sys-stack 524288 524288 +0.00% sys-total 103266608 102224216 -1.01% time 50339 46533 -7.56% virtual-mem 292990976 293728256 +0.25% garbage-1 allocated 2983818 2990889 +0.24% allocs 62880 62902 +0.03% cputime 16480000 16190000 -1.76% gc-pause-one 828462467 487875135 -41.11% gc-pause-total 4142312 2439375 -41.11% rss 1151709184 1153712128 +0.17% sys-gc 66068352 66068352 +0.00% sys-heap 1039728640 1039728640 +0.00% sys-other 37776064 40770176 +7.93% sys-stack 8781824 8781824 +0.00% sys-total 1152354880 1155348992 +0.26% time 16496998 16199876 -1.80% virtual-mem 1409564672 1402281984 -0.52% LGTM=rsc R=golang-codereviews, sameer, rsc, iant, jeremyjackins, gobot CC=golang-codereviews, khr https://golang.org/cl/46430043
2014-02-12 11:16:42 -07:00
return npages;
}
}
void
runtime·gchelper(void)
{
uint32 nproc;
2014-06-26 09:54:39 -06:00
g->m->traceback = 2;
gchelperstart();
runtime: faster GC mark phase Also bump MaxGcproc to 8. benchmark old ns/op new ns/op delta Parser 3796323000 3763880000 -0.85% Parser-2 3591752500 3518560250 -2.04% Parser-4 3423825250 3334955250 -2.60% Parser-8 3304585500 3267014750 -1.14% Parser-16 3313615750 3286160500 -0.83% Tree 984128500 942501166 -4.23% Tree-2 932564444 883266222 -5.29% Tree-4 835831000 799912777 -4.30% Tree-8 819238500 789717333 -3.73% Tree-16 880837833 837840055 -5.13% Tree2 604698100 579716900 -4.13% Tree2-2 372414500 356765200 -4.20% Tree2-4 187488100 177455900 -5.56% Tree2-8 136315300 102086700 -25.11% Tree2-16 93725900 76705800 -22.18% ParserPause 157441210 166202783 +5.56% ParserPause-2 93842650 85199900 -9.21% ParserPause-4 56844404 53535684 -5.82% ParserPause-8 35739446 30767613 -16.15% ParserPause-16 32718255 27212441 -16.83% TreePause 29610557 29787725 +0.60% TreePause-2 24001659 20674421 -13.86% TreePause-4 15114887 12842781 -15.03% TreePause-8 13128725 10741747 -22.22% TreePause-16 16131360 12506901 -22.47% Tree2Pause 2673350920 2651045280 -0.83% Tree2Pause-2 1796999200 1709350040 -4.88% Tree2Pause-4 1163553320 1090706480 -6.67% Tree2Pause-8 987032520 858916360 -25.11% Tree2Pause-16 864758560 809567480 -6.81% ParserLastPause 280537000 289047000 +3.03% ParserLastPause-2 183030000 166748000 -8.90% ParserLastPause-4 105817000 91552000 -13.48% ParserLastPause-8 65127000 53288000 -18.18% ParserLastPause-16 45258000 38334000 -15.30% TreeLastPause 45072000 51449000 +12.39% TreeLastPause-2 39269000 37866000 -3.57% TreeLastPause-4 23564000 20649000 -12.37% TreeLastPause-8 20881000 15807000 -24.30% TreeLastPause-16 23297000 17309000 -25.70% Tree2LastPause 6046912000 5797120000 -4.13% Tree2LastPause-2 3724034000 3567592000 -4.20% Tree2LastPause-4 1874831000 1774524000 -5.65% Tree2LastPause-8 1363108000 1020809000 -12.79% Tree2LastPause-16 937208000 767019000 -22.18% R=rsc, 0xe2.0x9a.0x9b CC=golang-dev https://golang.org/cl/6223050
2012-05-24 00:55:50 -06:00
// parallel mark for over gc roots
runtime·parfordo(work.markfor);
runtime: faster GC mark phase Also bump MaxGcproc to 8. benchmark old ns/op new ns/op delta Parser 3796323000 3763880000 -0.85% Parser-2 3591752500 3518560250 -2.04% Parser-4 3423825250 3334955250 -2.60% Parser-8 3304585500 3267014750 -1.14% Parser-16 3313615750 3286160500 -0.83% Tree 984128500 942501166 -4.23% Tree-2 932564444 883266222 -5.29% Tree-4 835831000 799912777 -4.30% Tree-8 819238500 789717333 -3.73% Tree-16 880837833 837840055 -5.13% Tree2 604698100 579716900 -4.13% Tree2-2 372414500 356765200 -4.20% Tree2-4 187488100 177455900 -5.56% Tree2-8 136315300 102086700 -25.11% Tree2-16 93725900 76705800 -22.18% ParserPause 157441210 166202783 +5.56% ParserPause-2 93842650 85199900 -9.21% ParserPause-4 56844404 53535684 -5.82% ParserPause-8 35739446 30767613 -16.15% ParserPause-16 32718255 27212441 -16.83% TreePause 29610557 29787725 +0.60% TreePause-2 24001659 20674421 -13.86% TreePause-4 15114887 12842781 -15.03% TreePause-8 13128725 10741747 -22.22% TreePause-16 16131360 12506901 -22.47% Tree2Pause 2673350920 2651045280 -0.83% Tree2Pause-2 1796999200 1709350040 -4.88% Tree2Pause-4 1163553320 1090706480 -6.67% Tree2Pause-8 987032520 858916360 -25.11% Tree2Pause-16 864758560 809567480 -6.81% ParserLastPause 280537000 289047000 +3.03% ParserLastPause-2 183030000 166748000 -8.90% ParserLastPause-4 105817000 91552000 -13.48% ParserLastPause-8 65127000 53288000 -18.18% ParserLastPause-16 45258000 38334000 -15.30% TreeLastPause 45072000 51449000 +12.39% TreeLastPause-2 39269000 37866000 -3.57% TreeLastPause-4 23564000 20649000 -12.37% TreeLastPause-8 20881000 15807000 -24.30% TreeLastPause-16 23297000 17309000 -25.70% Tree2LastPause 6046912000 5797120000 -4.13% Tree2LastPause-2 3724034000 3567592000 -4.20% Tree2LastPause-4 1874831000 1774524000 -5.65% Tree2LastPause-8 1363108000 1020809000 -12.79% Tree2LastPause-16 937208000 767019000 -22.18% R=rsc, 0xe2.0x9a.0x9b CC=golang-dev https://golang.org/cl/6223050
2012-05-24 00:55:50 -06:00
// help other threads scan secondary blocks
scanblock(nil, 0, nil);
nproc = work.nproc; // work.nproc can change right after we increment work.ndone
if(runtime·xadd(&work.ndone, +1) == nproc-1)
runtime·notewakeup(&work.alldone);
2014-06-26 09:54:39 -06:00
g->m->traceback = 0;
}
static void
cachestats(void)
{
MCache *c;
P *p, **pp;
for(pp=runtime·allp; p=*pp; pp++) {
c = p->mcache;
if(c==nil)
continue;
runtime·purgecachedstats(c);
}
}
static void
flushallmcaches(void)
{
P *p, **pp;
MCache *c;
// Flush MCache's to MCentral.
for(pp=runtime·allp; p=*pp; pp++) {
c = p->mcache;
if(c==nil)
continue;
runtime·MCache_ReleaseAll(c);
runtime·stackcache_clear(c);
}
}
static void
flushallmcaches_m(G *gp)
{
flushallmcaches();
runtime·gogo(&gp->sched);
}
void
runtime·updatememstats(GCStats *stats)
{
M *mp;
MSpan *s;
int32 i;
uint64 smallfree;
runtime: make GC stats per-M This is factored out part of: https://golang.org/cl/5279048/ (Parallel GC) benchmark old ns/op new ns/op delta garbage.BenchmarkParser 3999106750 3975026500 -0.60% garbage.BenchmarkParser-2 3720553750 3719196500 -0.04% garbage.BenchmarkParser-4 3502857000 3474980500 -0.80% garbage.BenchmarkParser-8 3375448000 3341310500 -1.01% garbage.BenchmarkParserLastPause 329401000 324097000 -1.61% garbage.BenchmarkParserLastPause-2 208953000 214222000 +2.52% garbage.BenchmarkParserLastPause-4 110933000 111656000 +0.65% garbage.BenchmarkParserLastPause-8 71969000 78230000 +8.70% garbage.BenchmarkParserPause 230808842 197237400 -14.55% garbage.BenchmarkParserPause-2 123674365 125197595 +1.23% garbage.BenchmarkParserPause-4 80518525 85710333 +6.45% garbage.BenchmarkParserPause-8 58310243 56940512 -2.35% garbage.BenchmarkTree2 31471700 31289400 -0.58% garbage.BenchmarkTree2-2 21536800 21086300 -2.09% garbage.BenchmarkTree2-4 11074700 10880000 -1.76% garbage.BenchmarkTree2-8 7568600 7351400 -2.87% garbage.BenchmarkTree2LastPause 314664000 312840000 -0.58% garbage.BenchmarkTree2LastPause-2 215319000 210815000 -2.09% garbage.BenchmarkTree2LastPause-4 110698000 108751000 -1.76% garbage.BenchmarkTree2LastPause-8 75635000 73463000 -2.87% garbage.BenchmarkTree2Pause 174280857 173147571 -0.65% garbage.BenchmarkTree2Pause-2 131332714 129665761 -1.27% garbage.BenchmarkTree2Pause-4 93803095 93422904 -0.41% garbage.BenchmarkTree2Pause-8 86242333 85146761 -1.27% R=rsc CC=golang-dev https://golang.org/cl/5987045
2012-04-05 10:48:28 -06:00
uint64 *src, *dst;
runtime: make GC stats per-M This is factored out part of: https://golang.org/cl/5279048/ (Parallel GC) benchmark old ns/op new ns/op delta garbage.BenchmarkParser 3999106750 3975026500 -0.60% garbage.BenchmarkParser-2 3720553750 3719196500 -0.04% garbage.BenchmarkParser-4 3502857000 3474980500 -0.80% garbage.BenchmarkParser-8 3375448000 3341310500 -1.01% garbage.BenchmarkParserLastPause 329401000 324097000 -1.61% garbage.BenchmarkParserLastPause-2 208953000 214222000 +2.52% garbage.BenchmarkParserLastPause-4 110933000 111656000 +0.65% garbage.BenchmarkParserLastPause-8 71969000 78230000 +8.70% garbage.BenchmarkParserPause 230808842 197237400 -14.55% garbage.BenchmarkParserPause-2 123674365 125197595 +1.23% garbage.BenchmarkParserPause-4 80518525 85710333 +6.45% garbage.BenchmarkParserPause-8 58310243 56940512 -2.35% garbage.BenchmarkTree2 31471700 31289400 -0.58% garbage.BenchmarkTree2-2 21536800 21086300 -2.09% garbage.BenchmarkTree2-4 11074700 10880000 -1.76% garbage.BenchmarkTree2-8 7568600 7351400 -2.87% garbage.BenchmarkTree2LastPause 314664000 312840000 -0.58% garbage.BenchmarkTree2LastPause-2 215319000 210815000 -2.09% garbage.BenchmarkTree2LastPause-4 110698000 108751000 -1.76% garbage.BenchmarkTree2LastPause-8 75635000 73463000 -2.87% garbage.BenchmarkTree2Pause 174280857 173147571 -0.65% garbage.BenchmarkTree2Pause-2 131332714 129665761 -1.27% garbage.BenchmarkTree2Pause-4 93803095 93422904 -0.41% garbage.BenchmarkTree2Pause-8 86242333 85146761 -1.27% R=rsc CC=golang-dev https://golang.org/cl/5987045
2012-04-05 10:48:28 -06:00
if(stats)
runtime·memclr((byte*)stats, sizeof(*stats));
for(mp=runtime·allm; mp; mp=mp->alllink) {
runtime: make GC stats per-M This is factored out part of: https://golang.org/cl/5279048/ (Parallel GC) benchmark old ns/op new ns/op delta garbage.BenchmarkParser 3999106750 3975026500 -0.60% garbage.BenchmarkParser-2 3720553750 3719196500 -0.04% garbage.BenchmarkParser-4 3502857000 3474980500 -0.80% garbage.BenchmarkParser-8 3375448000 3341310500 -1.01% garbage.BenchmarkParserLastPause 329401000 324097000 -1.61% garbage.BenchmarkParserLastPause-2 208953000 214222000 +2.52% garbage.BenchmarkParserLastPause-4 110933000 111656000 +0.65% garbage.BenchmarkParserLastPause-8 71969000 78230000 +8.70% garbage.BenchmarkParserPause 230808842 197237400 -14.55% garbage.BenchmarkParserPause-2 123674365 125197595 +1.23% garbage.BenchmarkParserPause-4 80518525 85710333 +6.45% garbage.BenchmarkParserPause-8 58310243 56940512 -2.35% garbage.BenchmarkTree2 31471700 31289400 -0.58% garbage.BenchmarkTree2-2 21536800 21086300 -2.09% garbage.BenchmarkTree2-4 11074700 10880000 -1.76% garbage.BenchmarkTree2-8 7568600 7351400 -2.87% garbage.BenchmarkTree2LastPause 314664000 312840000 -0.58% garbage.BenchmarkTree2LastPause-2 215319000 210815000 -2.09% garbage.BenchmarkTree2LastPause-4 110698000 108751000 -1.76% garbage.BenchmarkTree2LastPause-8 75635000 73463000 -2.87% garbage.BenchmarkTree2Pause 174280857 173147571 -0.65% garbage.BenchmarkTree2Pause-2 131332714 129665761 -1.27% garbage.BenchmarkTree2Pause-4 93803095 93422904 -0.41% garbage.BenchmarkTree2Pause-8 86242333 85146761 -1.27% R=rsc CC=golang-dev https://golang.org/cl/5987045
2012-04-05 10:48:28 -06:00
if(stats) {
src = (uint64*)&mp->gcstats;
runtime: make GC stats per-M This is factored out part of: https://golang.org/cl/5279048/ (Parallel GC) benchmark old ns/op new ns/op delta garbage.BenchmarkParser 3999106750 3975026500 -0.60% garbage.BenchmarkParser-2 3720553750 3719196500 -0.04% garbage.BenchmarkParser-4 3502857000 3474980500 -0.80% garbage.BenchmarkParser-8 3375448000 3341310500 -1.01% garbage.BenchmarkParserLastPause 329401000 324097000 -1.61% garbage.BenchmarkParserLastPause-2 208953000 214222000 +2.52% garbage.BenchmarkParserLastPause-4 110933000 111656000 +0.65% garbage.BenchmarkParserLastPause-8 71969000 78230000 +8.70% garbage.BenchmarkParserPause 230808842 197237400 -14.55% garbage.BenchmarkParserPause-2 123674365 125197595 +1.23% garbage.BenchmarkParserPause-4 80518525 85710333 +6.45% garbage.BenchmarkParserPause-8 58310243 56940512 -2.35% garbage.BenchmarkTree2 31471700 31289400 -0.58% garbage.BenchmarkTree2-2 21536800 21086300 -2.09% garbage.BenchmarkTree2-4 11074700 10880000 -1.76% garbage.BenchmarkTree2-8 7568600 7351400 -2.87% garbage.BenchmarkTree2LastPause 314664000 312840000 -0.58% garbage.BenchmarkTree2LastPause-2 215319000 210815000 -2.09% garbage.BenchmarkTree2LastPause-4 110698000 108751000 -1.76% garbage.BenchmarkTree2LastPause-8 75635000 73463000 -2.87% garbage.BenchmarkTree2Pause 174280857 173147571 -0.65% garbage.BenchmarkTree2Pause-2 131332714 129665761 -1.27% garbage.BenchmarkTree2Pause-4 93803095 93422904 -0.41% garbage.BenchmarkTree2Pause-8 86242333 85146761 -1.27% R=rsc CC=golang-dev https://golang.org/cl/5987045
2012-04-05 10:48:28 -06:00
dst = (uint64*)stats;
for(i=0; i<sizeof(*stats)/sizeof(uint64); i++)
dst[i] += src[i];
runtime·memclr((byte*)&mp->gcstats, sizeof(mp->gcstats));
runtime: make GC stats per-M This is factored out part of: https://golang.org/cl/5279048/ (Parallel GC) benchmark old ns/op new ns/op delta garbage.BenchmarkParser 3999106750 3975026500 -0.60% garbage.BenchmarkParser-2 3720553750 3719196500 -0.04% garbage.BenchmarkParser-4 3502857000 3474980500 -0.80% garbage.BenchmarkParser-8 3375448000 3341310500 -1.01% garbage.BenchmarkParserLastPause 329401000 324097000 -1.61% garbage.BenchmarkParserLastPause-2 208953000 214222000 +2.52% garbage.BenchmarkParserLastPause-4 110933000 111656000 +0.65% garbage.BenchmarkParserLastPause-8 71969000 78230000 +8.70% garbage.BenchmarkParserPause 230808842 197237400 -14.55% garbage.BenchmarkParserPause-2 123674365 125197595 +1.23% garbage.BenchmarkParserPause-4 80518525 85710333 +6.45% garbage.BenchmarkParserPause-8 58310243 56940512 -2.35% garbage.BenchmarkTree2 31471700 31289400 -0.58% garbage.BenchmarkTree2-2 21536800 21086300 -2.09% garbage.BenchmarkTree2-4 11074700 10880000 -1.76% garbage.BenchmarkTree2-8 7568600 7351400 -2.87% garbage.BenchmarkTree2LastPause 314664000 312840000 -0.58% garbage.BenchmarkTree2LastPause-2 215319000 210815000 -2.09% garbage.BenchmarkTree2LastPause-4 110698000 108751000 -1.76% garbage.BenchmarkTree2LastPause-8 75635000 73463000 -2.87% garbage.BenchmarkTree2Pause 174280857 173147571 -0.65% garbage.BenchmarkTree2Pause-2 131332714 129665761 -1.27% garbage.BenchmarkTree2Pause-4 93803095 93422904 -0.41% garbage.BenchmarkTree2Pause-8 86242333 85146761 -1.27% R=rsc CC=golang-dev https://golang.org/cl/5987045
2012-04-05 10:48:28 -06:00
}
}
mstats.mcache_inuse = runtime·mheap.cachealloc.inuse;
mstats.mspan_inuse = runtime·mheap.spanalloc.inuse;
mstats.sys = mstats.heap_sys + mstats.stacks_sys + mstats.mspan_sys +
mstats.mcache_sys + mstats.buckhash_sys + mstats.gc_sys + mstats.other_sys;
// Calculate memory allocator stats.
// During program execution we only count number of frees and amount of freed memory.
// Current number of alive object in the heap and amount of alive heap memory
// are calculated by scanning all spans.
// Total number of mallocs is calculated as number of frees plus number of alive objects.
// Similarly, total amount of allocated memory is calculated as amount of freed memory
// plus amount of alive heap memory.
mstats.alloc = 0;
mstats.total_alloc = 0;
mstats.nmalloc = 0;
mstats.nfree = 0;
for(i = 0; i < nelem(mstats.by_size); i++) {
mstats.by_size[i].nmalloc = 0;
mstats.by_size[i].nfree = 0;
}
// Flush MCache's to MCentral.
if(g == g->m->g0)
flushallmcaches();
else
runtime·mcall(flushallmcaches_m);
// Aggregate local stats.
cachestats();
// Scan all spans and count number of alive objects.
for(i = 0; i < runtime·mheap.nspan; i++) {
s = runtime·mheap.allspans[i];
if(s->state != MSpanInUse)
continue;
if(s->sizeclass == 0) {
mstats.nmalloc++;
mstats.alloc += s->elemsize;
} else {
mstats.nmalloc += s->ref;
mstats.by_size[s->sizeclass].nmalloc += s->ref;
mstats.alloc += s->ref*s->elemsize;
}
}
// Aggregate by size class.
smallfree = 0;
mstats.nfree = runtime·mheap.nlargefree;
for(i = 0; i < nelem(mstats.by_size); i++) {
mstats.nfree += runtime·mheap.nsmallfree[i];
mstats.by_size[i].nfree = runtime·mheap.nsmallfree[i];
mstats.by_size[i].nmalloc += runtime·mheap.nsmallfree[i];
smallfree += runtime·mheap.nsmallfree[i] * runtime·class_to_size[i];
}
mstats.nmalloc += mstats.nfree;
// Calculate derived stats.
mstats.total_alloc = mstats.alloc + runtime·mheap.largefree + smallfree;
mstats.heap_alloc = mstats.alloc;
mstats.heap_objects = mstats.nmalloc - mstats.nfree;
}
// Structure of arguments passed to function gc().
// This allows the arguments to be passed via runtime·mcall.
struct gc_args
{
int64 start_time; // start time of GC in ns (just before stoptheworld)
bool eagersweep;
};
static void gc(struct gc_args *args);
static void mgc(G *gp);
int32
runtime·readgogc(void)
{
byte *p;
p = runtime·getenv("GOGC");
if(p == nil || p[0] == '\0')
return 100;
if(runtime·strcmp(p, (byte*)"off") == 0)
return -1;
return runtime·atoi(p);
}
void
runtime·gcinit(void)
{
if(sizeof(Workbuf) != WorkbufSize)
runtime·throw("runtime: size of Workbuf is suboptimal");
work.markfor = runtime·parforalloc(MaxGcproc);
runtime·gcpercent = runtime·readgogc();
runtime·gcdatamask = unrollglobgcprog(gcdata, edata - data);
runtime·gcbssmask = unrollglobgcprog(gcbss, ebss - bss);
}
// force = 1 - do GC regardless of current heap usage
// force = 2 - go GC and eager sweep
void
runtime·gc(int32 force)
{
struct gc_args a;
int32 i;
// The gc is turned off (via enablegc) until
// the bootstrap has completed.
// Also, malloc gets called in the guts
// of a number of libraries that might be
// holding locks. To avoid priority inversion
// problems, don't bother trying to run gc
// while holding a lock. The next mallocgc
// without a lock will do the gc instead.
2014-06-26 09:54:39 -06:00
if(!mstats.enablegc || g == g->m->g0 || g->m->locks > 0 || runtime·panicking)
return;
if(runtime·gcpercent < 0)
return;
runtime·semacquire(&runtime·worldsema, false);
if(force==0 && mstats.heap_alloc < mstats.next_gc) {
// typically threads which lost the race to grab
// worldsema exit here when gc is done.
runtime·semrelease(&runtime·worldsema);
return;
}
// Ok, we're doing it! Stop everybody else
a.start_time = runtime·nanotime();
a.eagersweep = force >= 2;
2014-06-26 09:54:39 -06:00
g->m->gcing = 1;
runtime·stoptheworld();
runtime·clearpools();
// Run gc on the g0 stack. We do this so that the g stack
// we're currently running on will no longer change. Cuts
// the root set down a bit (g0 stacks are not scanned, and
// we don't need to scan gc's internal state). Also an
// enabler for copyable stacks.
for(i = 0; i < (runtime·debug.gctrace > 1 ? 2 : 1); i++) {
if(i > 0)
a.start_time = runtime·nanotime();
// switch to g0, call gc(&a), then switch back
g->param = &a;
g->status = Gwaiting;
g->waitreason = "garbage collection";
runtime·mcall(mgc);
}
// all done
2014-06-26 09:54:39 -06:00
g->m->gcing = 0;
g->m->locks++;
runtime·semrelease(&runtime·worldsema);
runtime·starttheworld();
2014-06-26 09:54:39 -06:00
g->m->locks--;
// now that gc is done, kick off finalizer thread if needed
if(!ConcurrentSweep) {
// give the queued finalizers, if any, a chance to run
runtime·gosched();
}
}
static void
mgc(G *gp)
{
gc(gp->param);
gp->param = nil;
runtime: record proper goroutine state during stack split Until now, the goroutine state has been scattered during the execution of newstack and oldstack. It's all there, and those routines know how to get back to a working goroutine, but other pieces of the system, like stack traces, do not. If something does interrupt the newstack or oldstack execution, the rest of the system can't understand the goroutine. For example, if newstack decides there is an overflow and calls throw, the stack tracer wouldn't dump the goroutine correctly. For newstack to save a useful state snapshot, it needs to be able to rewind the PC in the function that triggered the split back to the beginning of the function. (The PC is a few instructions in, just after the call to morestack.) To make that possible, we change the prologues to insert a jmp back to the beginning of the function after the call to morestack. That is, the prologue used to be roughly: TEXT myfunc check for split jmpcond nosplit call morestack nosplit: sub $xxx, sp Now an extra instruction is inserted after the call: TEXT myfunc start: check for split jmpcond nosplit call morestack jmp start nosplit: sub $xxx, sp The jmp is not executed directly. It is decoded and simulated by runtime.rewindmorestack to discover the beginning of the function, and then the call to morestack returns directly to the start label instead of to the jump instruction. So logically the jmp is still executed, just not by the cpu. The prologue thus repeats in the case of a function that needs a stack split, but against the cost of the split itself, the extra few instructions are noise. The repeated prologue has the nice effect of making a stack split double-check that the new stack is big enough: if morestack happens to return on a too-small stack, we'll now notice before corruption happens. The ability for newstack to rewind to the beginning of the function should help preemption too. If newstack decides that it was called for preemption instead of a stack split, it now has the goroutine state correctly paused if rescheduling is needed, and when the goroutine can run again, it can return to the start label on its original stack and re-execute the split check. Here is an example of a split stack overflow showing the full trace, without any special cases in the stack printer. (This one was triggered by making the split check incorrect.) runtime: newstack framesize=0x0 argsize=0x18 sp=0x6aebd0 stack=[0x6b0000, 0x6b0fa0] morebuf={pc:0x69f5b sp:0x6aebd8 lr:0x0} sched={pc:0x68880 sp:0x6aebd0 lr:0x0 ctxt:0x34e700} runtime: split stack overflow: 0x6aebd0 < 0x6b0000 fatal error: runtime: split stack overflow goroutine 1 [stack split]: runtime.mallocgc(0x290, 0x100000000, 0x1) /Users/rsc/g/go/src/pkg/runtime/zmalloc_darwin_amd64.c:21 fp=0x6aebd8 runtime.new() /Users/rsc/g/go/src/pkg/runtime/zmalloc_darwin_amd64.c:682 +0x5b fp=0x6aec08 go/build.(*Context).Import(0x5ae340, 0xc210030c71, 0xa, 0xc2100b4380, 0x1b, ...) /Users/rsc/g/go/src/pkg/go/build/build.go:424 +0x3a fp=0x6b00a0 main.loadImport(0xc210030c71, 0xa, 0xc2100b4380, 0x1b, 0xc2100b42c0, ...) /Users/rsc/g/go/src/cmd/go/pkg.go:249 +0x371 fp=0x6b01a8 main.(*Package).load(0xc21017c800, 0xc2100b42c0, 0xc2101828c0, 0x0, 0x0, ...) /Users/rsc/g/go/src/cmd/go/pkg.go:431 +0x2801 fp=0x6b0c98 main.loadPackage(0x369040, 0x7, 0xc2100b42c0, 0x0) /Users/rsc/g/go/src/cmd/go/pkg.go:709 +0x857 fp=0x6b0f80 ----- stack segment boundary ----- main.(*builder).action(0xc2100902a0, 0x0, 0x0, 0xc2100e6c00, 0xc2100e5750, ...) /Users/rsc/g/go/src/cmd/go/build.go:539 +0x437 fp=0x6b14a0 main.(*builder).action(0xc2100902a0, 0x0, 0x0, 0xc21015b400, 0x2, ...) /Users/rsc/g/go/src/cmd/go/build.go:528 +0x1d2 fp=0x6b1658 main.(*builder).test(0xc2100902a0, 0xc210092000, 0x0, 0x0, 0xc21008ff60, ...) /Users/rsc/g/go/src/cmd/go/test.go:622 +0x1b53 fp=0x6b1f68 ----- stack segment boundary ----- main.runTest(0x5a6b20, 0xc21000a020, 0x2, 0x2) /Users/rsc/g/go/src/cmd/go/test.go:366 +0xd09 fp=0x6a5cf0 main.main() /Users/rsc/g/go/src/cmd/go/main.go:161 +0x4f9 fp=0x6a5f78 runtime.main() /Users/rsc/g/go/src/pkg/runtime/proc.c:183 +0x92 fp=0x6a5fa0 runtime.goexit() /Users/rsc/g/go/src/pkg/runtime/proc.c:1266 fp=0x6a5fa8 And here is a seg fault during oldstack: SIGSEGV: segmentation violation PC=0x1b2a6 runtime.oldstack() /Users/rsc/g/go/src/pkg/runtime/stack.c:159 +0x76 runtime.lessstack() /Users/rsc/g/go/src/pkg/runtime/asm_amd64.s:270 +0x22 goroutine 1 [stack unsplit]: fmt.(*pp).printArg(0x2102e64e0, 0xe5c80, 0x2102c9220, 0x73, 0x0, ...) /Users/rsc/g/go/src/pkg/fmt/print.go:818 +0x3d3 fp=0x221031e6f8 fmt.(*pp).doPrintf(0x2102e64e0, 0x12fb20, 0x2, 0x221031eb98, 0x1, ...) /Users/rsc/g/go/src/pkg/fmt/print.go:1183 +0x15cb fp=0x221031eaf0 fmt.Sprintf(0x12fb20, 0x2, 0x221031eb98, 0x1, 0x1, ...) /Users/rsc/g/go/src/pkg/fmt/print.go:234 +0x67 fp=0x221031eb40 flag.(*stringValue).String(0x2102c9210, 0x1, 0x0) /Users/rsc/g/go/src/pkg/flag/flag.go:180 +0xb3 fp=0x221031ebb0 flag.(*FlagSet).Var(0x2102f6000, 0x293d38, 0x2102c9210, 0x143490, 0xa, ...) /Users/rsc/g/go/src/pkg/flag/flag.go:633 +0x40 fp=0x221031eca0 flag.(*FlagSet).StringVar(0x2102f6000, 0x2102c9210, 0x143490, 0xa, 0x12fa60, ...) /Users/rsc/g/go/src/pkg/flag/flag.go:550 +0x91 fp=0x221031ece8 flag.(*FlagSet).String(0x2102f6000, 0x143490, 0xa, 0x12fa60, 0x0, ...) /Users/rsc/g/go/src/pkg/flag/flag.go:563 +0x87 fp=0x221031ed38 flag.String(0x143490, 0xa, 0x12fa60, 0x0, 0x161950, ...) /Users/rsc/g/go/src/pkg/flag/flag.go:570 +0x6b fp=0x221031ed80 testing.init() /Users/rsc/g/go/src/pkg/testing/testing.go:-531 +0xbb fp=0x221031edc0 strings_test.init() /Users/rsc/g/go/src/pkg/strings/strings_test.go:1115 +0x62 fp=0x221031ef70 main.init() strings/_test/_testmain.go:90 +0x3d fp=0x221031ef78 runtime.main() /Users/rsc/g/go/src/pkg/runtime/proc.c:180 +0x8a fp=0x221031efa0 runtime.goexit() /Users/rsc/g/go/src/pkg/runtime/proc.c:1269 fp=0x221031efa8 goroutine 2 [runnable]: runtime.MHeap_Scavenger() /Users/rsc/g/go/src/pkg/runtime/mheap.c:438 runtime.goexit() /Users/rsc/g/go/src/pkg/runtime/proc.c:1269 created by runtime.main /Users/rsc/g/go/src/pkg/runtime/proc.c:166 rax 0x23ccc0 rbx 0x23ccc0 rcx 0x0 rdx 0x38 rdi 0x2102c0170 rsi 0x221032cfe0 rbp 0x221032cfa0 rsp 0x7fff5fbff5b0 r8 0x2102c0120 r9 0x221032cfa0 r10 0x221032c000 r11 0x104ce8 r12 0xe5c80 r13 0x1be82baac718 r14 0x13091135f7d69200 r15 0x0 rip 0x1b2a6 rflags 0x10246 cs 0x2b fs 0x0 gs 0x0 Fixes #5723. R=r, dvyukov, go.peter.90, dave, iant CC=golang-dev https://golang.org/cl/10360048
2013-06-27 09:32:01 -06:00
gp->status = Grunning;
runtime·gogo(&gp->sched);
}
void
runtime·gc_m(void)
{
struct gc_args a;
G *gp;
gp = g->m->curg;
gp->status = Gwaiting;
gp->waitreason = "garbage collection";
a.start_time = (uint64)(g->m->scalararg[0]) | ((uint64)(g->m->scalararg[1]) << 32);
a.eagersweep = g->m->scalararg[2];
gc(&a);
gp->status = Grunning;
}
static void
gc(struct gc_args *args)
{
int64 t0, t1, t2, t3, t4;
uint64 heap0, heap1, obj;
GCStats stats;
runtime: adjust GODEBUG=allocfreetrace=1 and GODEBUG=gcdead=1 GODEBUG=allocfreetrace=1: The allocfreetrace=1 mode prints a stack trace for each block allocated and freed, and also a stack trace for each garbage collection. It was implemented by reusing the heap profiling support: if allocfreetrace=1 then the heap profile was effectively running at 1 sample per 1 byte allocated (always sample). The stack being shown at allocation was the stack gathered for profiling, meaning it was derived only from the program counters and did not include information about function arguments or frame pointers. The stack being shown at free was the allocation stack, not the free stack. If you are generating this log, you can find the allocation stack yourself, but it can be useful to see exactly the sequence that led to freeing the block: was it the garbage collector or an explicit free? Now that the garbage collector runs on an m0 stack, the stack trace for the garbage collector was never interesting. Fix all these problems: 1. Decouple allocfreetrace=1 from heap profiling. 2. Print the standard goroutine stack traces instead of a custom format. 3. Print the stack trace at time of allocation for an allocation, and print the stack trace at time of free (not the allocation trace again) for a free. 4. Print all goroutine stacks at garbage collection. Having all the stacks means that you can see the exact point at which each goroutine was preempted, which is often useful for identifying liveness-related errors. GODEBUG=gcdead=1: This mode overwrites dead pointers with a poison value. Detect the poison value as an invalid pointer during collection, the same way that small integers are invalid pointers. LGTM=khr R=khr CC=golang-codereviews https://golang.org/cl/81670043
2014-04-01 11:30:10 -06:00
if(runtime·debug.allocfreetrace)
runtime·tracegc();
2014-06-26 09:54:39 -06:00
g->m->traceback = 2;
t0 = args->start_time;
work.tstart = args->start_time;
t1 = 0;
if(runtime·debug.gctrace)
t1 = runtime·nanotime();
runtime: concurrent GC sweep Moves sweep phase out of stoptheworld by adding background sweeper goroutine and lazy on-demand sweeping. It turned out to be somewhat trickier than I expected, because there is no point in time when we know size of live heap nor consistent number of mallocs and frees. So everything related to next_gc, mprof, memstats, etc becomes trickier. At the end of GC next_gc is conservatively set to heap_alloc*GOGC, which is much larger than real value. But after every sweep next_gc is decremented by freed*GOGC. So when everything is swept next_gc becomes what it should be. For mprof I had to introduce 3-generation scheme (allocs, revent_allocs, prev_allocs), because by the end of GC we know number of frees for the *previous* GC. Significant caution is required to not cross yet-unknown real value of next_gc. This is achieved by 2 means: 1. Whenever I allocate a span from MCentral, I sweep a span in that MCentral. 2. Whenever I allocate N pages from MHeap, I sweep until at least N pages are returned to heap. This provides quite strong guarantees that heap does not grow when it should now. http-1 allocated 7036 7033 -0.04% allocs 60 60 +0.00% cputime 51050 46700 -8.52% gc-pause-one 34060569 1777993 -94.78% gc-pause-total 2554 133 -94.79% latency-50 178448 170926 -4.22% latency-95 284350 198294 -30.26% latency-99 345191 220652 -36.08% rss 101564416 101007360 -0.55% sys-gc 6606832 6541296 -0.99% sys-heap 88801280 87752704 -1.18% sys-other 7334208 7405928 +0.98% sys-stack 524288 524288 +0.00% sys-total 103266608 102224216 -1.01% time 50339 46533 -7.56% virtual-mem 292990976 293728256 +0.25% garbage-1 allocated 2983818 2990889 +0.24% allocs 62880 62902 +0.03% cputime 16480000 16190000 -1.76% gc-pause-one 828462467 487875135 -41.11% gc-pause-total 4142312 2439375 -41.11% rss 1151709184 1153712128 +0.17% sys-gc 66068352 66068352 +0.00% sys-heap 1039728640 1039728640 +0.00% sys-other 37776064 40770176 +7.93% sys-stack 8781824 8781824 +0.00% sys-total 1152354880 1155348992 +0.26% time 16496998 16199876 -1.80% virtual-mem 1409564672 1402281984 -0.52% LGTM=rsc R=golang-codereviews, sameer, rsc, iant, jeremyjackins, gobot CC=golang-codereviews, khr https://golang.org/cl/46430043
2014-02-12 11:16:42 -07:00
// Sweep what is not sweeped by bgsweep.
while(runtime·sweepone() != -1)
sweep.npausesweep++;
runtime: concurrent GC sweep Moves sweep phase out of stoptheworld by adding background sweeper goroutine and lazy on-demand sweeping. It turned out to be somewhat trickier than I expected, because there is no point in time when we know size of live heap nor consistent number of mallocs and frees. So everything related to next_gc, mprof, memstats, etc becomes trickier. At the end of GC next_gc is conservatively set to heap_alloc*GOGC, which is much larger than real value. But after every sweep next_gc is decremented by freed*GOGC. So when everything is swept next_gc becomes what it should be. For mprof I had to introduce 3-generation scheme (allocs, revent_allocs, prev_allocs), because by the end of GC we know number of frees for the *previous* GC. Significant caution is required to not cross yet-unknown real value of next_gc. This is achieved by 2 means: 1. Whenever I allocate a span from MCentral, I sweep a span in that MCentral. 2. Whenever I allocate N pages from MHeap, I sweep until at least N pages are returned to heap. This provides quite strong guarantees that heap does not grow when it should now. http-1 allocated 7036 7033 -0.04% allocs 60 60 +0.00% cputime 51050 46700 -8.52% gc-pause-one 34060569 1777993 -94.78% gc-pause-total 2554 133 -94.79% latency-50 178448 170926 -4.22% latency-95 284350 198294 -30.26% latency-99 345191 220652 -36.08% rss 101564416 101007360 -0.55% sys-gc 6606832 6541296 -0.99% sys-heap 88801280 87752704 -1.18% sys-other 7334208 7405928 +0.98% sys-stack 524288 524288 +0.00% sys-total 103266608 102224216 -1.01% time 50339 46533 -7.56% virtual-mem 292990976 293728256 +0.25% garbage-1 allocated 2983818 2990889 +0.24% allocs 62880 62902 +0.03% cputime 16480000 16190000 -1.76% gc-pause-one 828462467 487875135 -41.11% gc-pause-total 4142312 2439375 -41.11% rss 1151709184 1153712128 +0.17% sys-gc 66068352 66068352 +0.00% sys-heap 1039728640 1039728640 +0.00% sys-other 37776064 40770176 +7.93% sys-stack 8781824 8781824 +0.00% sys-total 1152354880 1155348992 +0.26% time 16496998 16199876 -1.80% virtual-mem 1409564672 1402281984 -0.52% LGTM=rsc R=golang-codereviews, sameer, rsc, iant, jeremyjackins, gobot CC=golang-codereviews, khr https://golang.org/cl/46430043
2014-02-12 11:16:42 -07:00
runtime: faster GC sweep phase benchmark old ns/op new ns/op delta garbage.BenchmarkParser 3731065750 3715543750 -0.41% garbage.BenchmarkParser-2 3631299750 3495248500 -3.75% garbage.BenchmarkParser-4 3386486000 3339353000 -1.39% garbage.BenchmarkParser-8 3267632000 3286422500 +0.58% garbage.BenchmarkParser-16 3299203000 3316081750 +0.51% garbage.BenchmarkTree 977532888 919453833 -5.94% garbage.BenchmarkTree-2 919948555 853478000 -7.23% garbage.BenchmarkTree-4 841329000 790207000 -6.08% garbage.BenchmarkTree-8 787792777 740380666 -6.01% garbage.BenchmarkTree-16 899257166 846594555 -5.86% garbage.BenchmarkTree2 574876300 571885800 -0.52% garbage.BenchmarkTree2-2 348162700 345888900 -0.65% garbage.BenchmarkTree2-4 184912500 179137000 -3.22% garbage.BenchmarkTree2-8 104243900 103485600 -0.73% garbage.BenchmarkTree2-16 97269500 85137100 -14.25% garbage.BenchmarkParserPause 141101976 157746974 +11.80% garbage.BenchmarkParserPause-2 103096051 83043048 -19.45% garbage.BenchmarkParserPause-4 52153133 45951111 -11.89% garbage.BenchmarkParserPause-8 36730190 38901024 +5.91% garbage.BenchmarkParserPause-16 32678875 29578585 -9.49% garbage.BenchmarkTreePause 29487065 29648439 +0.55% garbage.BenchmarkTreePause-2 22443494 21306159 -5.07% garbage.BenchmarkTreePause-4 15799691 14985647 -5.15% garbage.BenchmarkTreePause-8 10768112 9531420 -12.97% garbage.BenchmarkTreePause-16 16329891 15205158 -6.89% garbage.BenchmarkTree2Pause 2586957240 2577533200 -0.36% garbage.BenchmarkTree2Pause-2 1683383760 1673923800 -0.56% garbage.BenchmarkTree2Pause-4 1102860320 1074040280 -2.68% garbage.BenchmarkTree2Pause-8 902627920 886122400 -1.86% garbage.BenchmarkTree2Pause-16 856470920 804152320 -6.50% garbage.BenchmarkParserLastPause 277316000 280839000 +1.25% garbage.BenchmarkParserLastPause-2 179446000 163687000 -8.78% garbage.BenchmarkParserLastPause-4 106752000 94144000 -11.81% garbage.BenchmarkParserLastPause-8 57758000 61640000 +6.72% garbage.BenchmarkParserLastPause-16 51235000 42552000 -16.95% garbage.BenchmarkTreeLastPause 45244000 50786000 +12.25% garbage.BenchmarkTreeLastPause-2 37163000 34654000 -6.75% garbage.BenchmarkTreeLastPause-4 24178000 21967000 -9.14% garbage.BenchmarkTreeLastPause-8 20390000 15648000 -30.30% garbage.BenchmarkTreeLastPause-16 22398000 20180000 -9.90% garbage.BenchmarkTree2LastPause 5748706000 5718809000 -0.52% garbage.BenchmarkTree2LastPause-2 3481570000 3458844000 -0.65% garbage.BenchmarkTree2LastPause-4 1849073000 1791330000 -3.22% garbage.BenchmarkTree2LastPause-8 1042375000 1034811000 -0.73% garbage.BenchmarkTree2LastPause-16 972637000 851323000 -14.25% There is also visible improvement in consumed CPU time: tree2 -heapsize=8000000000 -cpus=12 before: 248.74user 6.36system 0:52.74elapsed 483%CPU after: 229.86user 6.33system 0:51.08elapsed 462%CPU -1.66s of real time, but -18.91s of consumed CPU time R=golang-dev CC=golang-dev https://golang.org/cl/6215065
2012-05-22 11:35:52 -06:00
work.nwait = 0;
work.ndone = 0;
work.nproc = runtime·gcprocs();
runtime·parforsetup(work.markfor, work.nproc, RootCount + runtime·allglen, nil, false, markroot);
if(work.nproc > 1) {
runtime·noteclear(&work.alldone);
runtime·helpgc(work.nproc);
}
t2 = 0;
if(runtime·debug.gctrace)
t2 = runtime·nanotime();
gchelperstart();
runtime: faster GC mark phase Also bump MaxGcproc to 8. benchmark old ns/op new ns/op delta Parser 3796323000 3763880000 -0.85% Parser-2 3591752500 3518560250 -2.04% Parser-4 3423825250 3334955250 -2.60% Parser-8 3304585500 3267014750 -1.14% Parser-16 3313615750 3286160500 -0.83% Tree 984128500 942501166 -4.23% Tree-2 932564444 883266222 -5.29% Tree-4 835831000 799912777 -4.30% Tree-8 819238500 789717333 -3.73% Tree-16 880837833 837840055 -5.13% Tree2 604698100 579716900 -4.13% Tree2-2 372414500 356765200 -4.20% Tree2-4 187488100 177455900 -5.56% Tree2-8 136315300 102086700 -25.11% Tree2-16 93725900 76705800 -22.18% ParserPause 157441210 166202783 +5.56% ParserPause-2 93842650 85199900 -9.21% ParserPause-4 56844404 53535684 -5.82% ParserPause-8 35739446 30767613 -16.15% ParserPause-16 32718255 27212441 -16.83% TreePause 29610557 29787725 +0.60% TreePause-2 24001659 20674421 -13.86% TreePause-4 15114887 12842781 -15.03% TreePause-8 13128725 10741747 -22.22% TreePause-16 16131360 12506901 -22.47% Tree2Pause 2673350920 2651045280 -0.83% Tree2Pause-2 1796999200 1709350040 -4.88% Tree2Pause-4 1163553320 1090706480 -6.67% Tree2Pause-8 987032520 858916360 -25.11% Tree2Pause-16 864758560 809567480 -6.81% ParserLastPause 280537000 289047000 +3.03% ParserLastPause-2 183030000 166748000 -8.90% ParserLastPause-4 105817000 91552000 -13.48% ParserLastPause-8 65127000 53288000 -18.18% ParserLastPause-16 45258000 38334000 -15.30% TreeLastPause 45072000 51449000 +12.39% TreeLastPause-2 39269000 37866000 -3.57% TreeLastPause-4 23564000 20649000 -12.37% TreeLastPause-8 20881000 15807000 -24.30% TreeLastPause-16 23297000 17309000 -25.70% Tree2LastPause 6046912000 5797120000 -4.13% Tree2LastPause-2 3724034000 3567592000 -4.20% Tree2LastPause-4 1874831000 1774524000 -5.65% Tree2LastPause-8 1363108000 1020809000 -12.79% Tree2LastPause-16 937208000 767019000 -22.18% R=rsc, 0xe2.0x9a.0x9b CC=golang-dev https://golang.org/cl/6223050
2012-05-24 00:55:50 -06:00
runtime·parfordo(work.markfor);
scanblock(nil, 0, nil);
runtime: faster GC mark phase Also bump MaxGcproc to 8. benchmark old ns/op new ns/op delta Parser 3796323000 3763880000 -0.85% Parser-2 3591752500 3518560250 -2.04% Parser-4 3423825250 3334955250 -2.60% Parser-8 3304585500 3267014750 -1.14% Parser-16 3313615750 3286160500 -0.83% Tree 984128500 942501166 -4.23% Tree-2 932564444 883266222 -5.29% Tree-4 835831000 799912777 -4.30% Tree-8 819238500 789717333 -3.73% Tree-16 880837833 837840055 -5.13% Tree2 604698100 579716900 -4.13% Tree2-2 372414500 356765200 -4.20% Tree2-4 187488100 177455900 -5.56% Tree2-8 136315300 102086700 -25.11% Tree2-16 93725900 76705800 -22.18% ParserPause 157441210 166202783 +5.56% ParserPause-2 93842650 85199900 -9.21% ParserPause-4 56844404 53535684 -5.82% ParserPause-8 35739446 30767613 -16.15% ParserPause-16 32718255 27212441 -16.83% TreePause 29610557 29787725 +0.60% TreePause-2 24001659 20674421 -13.86% TreePause-4 15114887 12842781 -15.03% TreePause-8 13128725 10741747 -22.22% TreePause-16 16131360 12506901 -22.47% Tree2Pause 2673350920 2651045280 -0.83% Tree2Pause-2 1796999200 1709350040 -4.88% Tree2Pause-4 1163553320 1090706480 -6.67% Tree2Pause-8 987032520 858916360 -25.11% Tree2Pause-16 864758560 809567480 -6.81% ParserLastPause 280537000 289047000 +3.03% ParserLastPause-2 183030000 166748000 -8.90% ParserLastPause-4 105817000 91552000 -13.48% ParserLastPause-8 65127000 53288000 -18.18% ParserLastPause-16 45258000 38334000 -15.30% TreeLastPause 45072000 51449000 +12.39% TreeLastPause-2 39269000 37866000 -3.57% TreeLastPause-4 23564000 20649000 -12.37% TreeLastPause-8 20881000 15807000 -24.30% TreeLastPause-16 23297000 17309000 -25.70% Tree2LastPause 6046912000 5797120000 -4.13% Tree2LastPause-2 3724034000 3567592000 -4.20% Tree2LastPause-4 1874831000 1774524000 -5.65% Tree2LastPause-8 1363108000 1020809000 -12.79% Tree2LastPause-16 937208000 767019000 -22.18% R=rsc, 0xe2.0x9a.0x9b CC=golang-dev https://golang.org/cl/6223050
2012-05-24 00:55:50 -06:00
t3 = 0;
if(runtime·debug.gctrace)
t3 = runtime·nanotime();
runtime: faster GC sweep phase benchmark old ns/op new ns/op delta garbage.BenchmarkParser 3731065750 3715543750 -0.41% garbage.BenchmarkParser-2 3631299750 3495248500 -3.75% garbage.BenchmarkParser-4 3386486000 3339353000 -1.39% garbage.BenchmarkParser-8 3267632000 3286422500 +0.58% garbage.BenchmarkParser-16 3299203000 3316081750 +0.51% garbage.BenchmarkTree 977532888 919453833 -5.94% garbage.BenchmarkTree-2 919948555 853478000 -7.23% garbage.BenchmarkTree-4 841329000 790207000 -6.08% garbage.BenchmarkTree-8 787792777 740380666 -6.01% garbage.BenchmarkTree-16 899257166 846594555 -5.86% garbage.BenchmarkTree2 574876300 571885800 -0.52% garbage.BenchmarkTree2-2 348162700 345888900 -0.65% garbage.BenchmarkTree2-4 184912500 179137000 -3.22% garbage.BenchmarkTree2-8 104243900 103485600 -0.73% garbage.BenchmarkTree2-16 97269500 85137100 -14.25% garbage.BenchmarkParserPause 141101976 157746974 +11.80% garbage.BenchmarkParserPause-2 103096051 83043048 -19.45% garbage.BenchmarkParserPause-4 52153133 45951111 -11.89% garbage.BenchmarkParserPause-8 36730190 38901024 +5.91% garbage.BenchmarkParserPause-16 32678875 29578585 -9.49% garbage.BenchmarkTreePause 29487065 29648439 +0.55% garbage.BenchmarkTreePause-2 22443494 21306159 -5.07% garbage.BenchmarkTreePause-4 15799691 14985647 -5.15% garbage.BenchmarkTreePause-8 10768112 9531420 -12.97% garbage.BenchmarkTreePause-16 16329891 15205158 -6.89% garbage.BenchmarkTree2Pause 2586957240 2577533200 -0.36% garbage.BenchmarkTree2Pause-2 1683383760 1673923800 -0.56% garbage.BenchmarkTree2Pause-4 1102860320 1074040280 -2.68% garbage.BenchmarkTree2Pause-8 902627920 886122400 -1.86% garbage.BenchmarkTree2Pause-16 856470920 804152320 -6.50% garbage.BenchmarkParserLastPause 277316000 280839000 +1.25% garbage.BenchmarkParserLastPause-2 179446000 163687000 -8.78% garbage.BenchmarkParserLastPause-4 106752000 94144000 -11.81% garbage.BenchmarkParserLastPause-8 57758000 61640000 +6.72% garbage.BenchmarkParserLastPause-16 51235000 42552000 -16.95% garbage.BenchmarkTreeLastPause 45244000 50786000 +12.25% garbage.BenchmarkTreeLastPause-2 37163000 34654000 -6.75% garbage.BenchmarkTreeLastPause-4 24178000 21967000 -9.14% garbage.BenchmarkTreeLastPause-8 20390000 15648000 -30.30% garbage.BenchmarkTreeLastPause-16 22398000 20180000 -9.90% garbage.BenchmarkTree2LastPause 5748706000 5718809000 -0.52% garbage.BenchmarkTree2LastPause-2 3481570000 3458844000 -0.65% garbage.BenchmarkTree2LastPause-4 1849073000 1791330000 -3.22% garbage.BenchmarkTree2LastPause-8 1042375000 1034811000 -0.73% garbage.BenchmarkTree2LastPause-16 972637000 851323000 -14.25% There is also visible improvement in consumed CPU time: tree2 -heapsize=8000000000 -cpus=12 before: 248.74user 6.36system 0:52.74elapsed 483%CPU after: 229.86user 6.33system 0:51.08elapsed 462%CPU -1.66s of real time, but -18.91s of consumed CPU time R=golang-dev CC=golang-dev https://golang.org/cl/6215065
2012-05-22 11:35:52 -06:00
if(work.nproc > 1)
runtime·notesleep(&work.alldone);
cachestats();
runtime: concurrent GC sweep Moves sweep phase out of stoptheworld by adding background sweeper goroutine and lazy on-demand sweeping. It turned out to be somewhat trickier than I expected, because there is no point in time when we know size of live heap nor consistent number of mallocs and frees. So everything related to next_gc, mprof, memstats, etc becomes trickier. At the end of GC next_gc is conservatively set to heap_alloc*GOGC, which is much larger than real value. But after every sweep next_gc is decremented by freed*GOGC. So when everything is swept next_gc becomes what it should be. For mprof I had to introduce 3-generation scheme (allocs, revent_allocs, prev_allocs), because by the end of GC we know number of frees for the *previous* GC. Significant caution is required to not cross yet-unknown real value of next_gc. This is achieved by 2 means: 1. Whenever I allocate a span from MCentral, I sweep a span in that MCentral. 2. Whenever I allocate N pages from MHeap, I sweep until at least N pages are returned to heap. This provides quite strong guarantees that heap does not grow when it should now. http-1 allocated 7036 7033 -0.04% allocs 60 60 +0.00% cputime 51050 46700 -8.52% gc-pause-one 34060569 1777993 -94.78% gc-pause-total 2554 133 -94.79% latency-50 178448 170926 -4.22% latency-95 284350 198294 -30.26% latency-99 345191 220652 -36.08% rss 101564416 101007360 -0.55% sys-gc 6606832 6541296 -0.99% sys-heap 88801280 87752704 -1.18% sys-other 7334208 7405928 +0.98% sys-stack 524288 524288 +0.00% sys-total 103266608 102224216 -1.01% time 50339 46533 -7.56% virtual-mem 292990976 293728256 +0.25% garbage-1 allocated 2983818 2990889 +0.24% allocs 62880 62902 +0.03% cputime 16480000 16190000 -1.76% gc-pause-one 828462467 487875135 -41.11% gc-pause-total 4142312 2439375 -41.11% rss 1151709184 1153712128 +0.17% sys-gc 66068352 66068352 +0.00% sys-heap 1039728640 1039728640 +0.00% sys-other 37776064 40770176 +7.93% sys-stack 8781824 8781824 +0.00% sys-total 1152354880 1155348992 +0.26% time 16496998 16199876 -1.80% virtual-mem 1409564672 1402281984 -0.52% LGTM=rsc R=golang-codereviews, sameer, rsc, iant, jeremyjackins, gobot CC=golang-codereviews, khr https://golang.org/cl/46430043
2014-02-12 11:16:42 -07:00
// next_gc calculation is tricky with concurrent sweep since we don't know size of live heap
// estimate what was live heap size after previous GC (for tracing only)
heap0 = mstats.next_gc*100/(runtime·gcpercent+100);
runtime: concurrent GC sweep Moves sweep phase out of stoptheworld by adding background sweeper goroutine and lazy on-demand sweeping. It turned out to be somewhat trickier than I expected, because there is no point in time when we know size of live heap nor consistent number of mallocs and frees. So everything related to next_gc, mprof, memstats, etc becomes trickier. At the end of GC next_gc is conservatively set to heap_alloc*GOGC, which is much larger than real value. But after every sweep next_gc is decremented by freed*GOGC. So when everything is swept next_gc becomes what it should be. For mprof I had to introduce 3-generation scheme (allocs, revent_allocs, prev_allocs), because by the end of GC we know number of frees for the *previous* GC. Significant caution is required to not cross yet-unknown real value of next_gc. This is achieved by 2 means: 1. Whenever I allocate a span from MCentral, I sweep a span in that MCentral. 2. Whenever I allocate N pages from MHeap, I sweep until at least N pages are returned to heap. This provides quite strong guarantees that heap does not grow when it should now. http-1 allocated 7036 7033 -0.04% allocs 60 60 +0.00% cputime 51050 46700 -8.52% gc-pause-one 34060569 1777993 -94.78% gc-pause-total 2554 133 -94.79% latency-50 178448 170926 -4.22% latency-95 284350 198294 -30.26% latency-99 345191 220652 -36.08% rss 101564416 101007360 -0.55% sys-gc 6606832 6541296 -0.99% sys-heap 88801280 87752704 -1.18% sys-other 7334208 7405928 +0.98% sys-stack 524288 524288 +0.00% sys-total 103266608 102224216 -1.01% time 50339 46533 -7.56% virtual-mem 292990976 293728256 +0.25% garbage-1 allocated 2983818 2990889 +0.24% allocs 62880 62902 +0.03% cputime 16480000 16190000 -1.76% gc-pause-one 828462467 487875135 -41.11% gc-pause-total 4142312 2439375 -41.11% rss 1151709184 1153712128 +0.17% sys-gc 66068352 66068352 +0.00% sys-heap 1039728640 1039728640 +0.00% sys-other 37776064 40770176 +7.93% sys-stack 8781824 8781824 +0.00% sys-total 1152354880 1155348992 +0.26% time 16496998 16199876 -1.80% virtual-mem 1409564672 1402281984 -0.52% LGTM=rsc R=golang-codereviews, sameer, rsc, iant, jeremyjackins, gobot CC=golang-codereviews, khr https://golang.org/cl/46430043
2014-02-12 11:16:42 -07:00
// conservatively set next_gc to high value assuming that everything is live
// concurrent/lazy sweep will reduce this number while discovering new garbage
mstats.next_gc = mstats.heap_alloc+mstats.heap_alloc*runtime·gcpercent/100;
t4 = runtime·nanotime();
mstats.last_gc = runtime·unixnanotime(); // must be Unix time to make sense to user
mstats.pause_ns[mstats.numgc%nelem(mstats.pause_ns)] = t4 - t0;
mstats.pause_total_ns += t4 - t0;
mstats.numgc++;
if(mstats.debuggc)
runtime·printf("pause %D\n", t4-t0);
if(runtime·debug.gctrace) {
heap1 = mstats.heap_alloc;
runtime·updatememstats(&stats);
if(heap1 != mstats.heap_alloc) {
runtime·printf("runtime: mstats skew: heap=%D/%D\n", heap1, mstats.heap_alloc);
runtime·throw("mstats skew");
}
runtime: concurrent GC sweep Moves sweep phase out of stoptheworld by adding background sweeper goroutine and lazy on-demand sweeping. It turned out to be somewhat trickier than I expected, because there is no point in time when we know size of live heap nor consistent number of mallocs and frees. So everything related to next_gc, mprof, memstats, etc becomes trickier. At the end of GC next_gc is conservatively set to heap_alloc*GOGC, which is much larger than real value. But after every sweep next_gc is decremented by freed*GOGC. So when everything is swept next_gc becomes what it should be. For mprof I had to introduce 3-generation scheme (allocs, revent_allocs, prev_allocs), because by the end of GC we know number of frees for the *previous* GC. Significant caution is required to not cross yet-unknown real value of next_gc. This is achieved by 2 means: 1. Whenever I allocate a span from MCentral, I sweep a span in that MCentral. 2. Whenever I allocate N pages from MHeap, I sweep until at least N pages are returned to heap. This provides quite strong guarantees that heap does not grow when it should now. http-1 allocated 7036 7033 -0.04% allocs 60 60 +0.00% cputime 51050 46700 -8.52% gc-pause-one 34060569 1777993 -94.78% gc-pause-total 2554 133 -94.79% latency-50 178448 170926 -4.22% latency-95 284350 198294 -30.26% latency-99 345191 220652 -36.08% rss 101564416 101007360 -0.55% sys-gc 6606832 6541296 -0.99% sys-heap 88801280 87752704 -1.18% sys-other 7334208 7405928 +0.98% sys-stack 524288 524288 +0.00% sys-total 103266608 102224216 -1.01% time 50339 46533 -7.56% virtual-mem 292990976 293728256 +0.25% garbage-1 allocated 2983818 2990889 +0.24% allocs 62880 62902 +0.03% cputime 16480000 16190000 -1.76% gc-pause-one 828462467 487875135 -41.11% gc-pause-total 4142312 2439375 -41.11% rss 1151709184 1153712128 +0.17% sys-gc 66068352 66068352 +0.00% sys-heap 1039728640 1039728640 +0.00% sys-other 37776064 40770176 +7.93% sys-stack 8781824 8781824 +0.00% sys-total 1152354880 1155348992 +0.26% time 16496998 16199876 -1.80% virtual-mem 1409564672 1402281984 -0.52% LGTM=rsc R=golang-codereviews, sameer, rsc, iant, jeremyjackins, gobot CC=golang-codereviews, khr https://golang.org/cl/46430043
2014-02-12 11:16:42 -07:00
obj = mstats.nmalloc - mstats.nfree;
runtime: concurrent GC sweep Moves sweep phase out of stoptheworld by adding background sweeper goroutine and lazy on-demand sweeping. It turned out to be somewhat trickier than I expected, because there is no point in time when we know size of live heap nor consistent number of mallocs and frees. So everything related to next_gc, mprof, memstats, etc becomes trickier. At the end of GC next_gc is conservatively set to heap_alloc*GOGC, which is much larger than real value. But after every sweep next_gc is decremented by freed*GOGC. So when everything is swept next_gc becomes what it should be. For mprof I had to introduce 3-generation scheme (allocs, revent_allocs, prev_allocs), because by the end of GC we know number of frees for the *previous* GC. Significant caution is required to not cross yet-unknown real value of next_gc. This is achieved by 2 means: 1. Whenever I allocate a span from MCentral, I sweep a span in that MCentral. 2. Whenever I allocate N pages from MHeap, I sweep until at least N pages are returned to heap. This provides quite strong guarantees that heap does not grow when it should now. http-1 allocated 7036 7033 -0.04% allocs 60 60 +0.00% cputime 51050 46700 -8.52% gc-pause-one 34060569 1777993 -94.78% gc-pause-total 2554 133 -94.79% latency-50 178448 170926 -4.22% latency-95 284350 198294 -30.26% latency-99 345191 220652 -36.08% rss 101564416 101007360 -0.55% sys-gc 6606832 6541296 -0.99% sys-heap 88801280 87752704 -1.18% sys-other 7334208 7405928 +0.98% sys-stack 524288 524288 +0.00% sys-total 103266608 102224216 -1.01% time 50339 46533 -7.56% virtual-mem 292990976 293728256 +0.25% garbage-1 allocated 2983818 2990889 +0.24% allocs 62880 62902 +0.03% cputime 16480000 16190000 -1.76% gc-pause-one 828462467 487875135 -41.11% gc-pause-total 4142312 2439375 -41.11% rss 1151709184 1153712128 +0.17% sys-gc 66068352 66068352 +0.00% sys-heap 1039728640 1039728640 +0.00% sys-other 37776064 40770176 +7.93% sys-stack 8781824 8781824 +0.00% sys-total 1152354880 1155348992 +0.26% time 16496998 16199876 -1.80% virtual-mem 1409564672 1402281984 -0.52% LGTM=rsc R=golang-codereviews, sameer, rsc, iant, jeremyjackins, gobot CC=golang-codereviews, khr https://golang.org/cl/46430043
2014-02-12 11:16:42 -07:00
stats.nprocyield += work.markfor->nprocyield;
stats.nosyield += work.markfor->nosyield;
stats.nsleep += work.markfor->nsleep;
runtime·printf("gc%d(%d): %D+%D+%D+%D us, %D -> %D MB, %D (%D-%D) objects,"
runtime: concurrent GC sweep Moves sweep phase out of stoptheworld by adding background sweeper goroutine and lazy on-demand sweeping. It turned out to be somewhat trickier than I expected, because there is no point in time when we know size of live heap nor consistent number of mallocs and frees. So everything related to next_gc, mprof, memstats, etc becomes trickier. At the end of GC next_gc is conservatively set to heap_alloc*GOGC, which is much larger than real value. But after every sweep next_gc is decremented by freed*GOGC. So when everything is swept next_gc becomes what it should be. For mprof I had to introduce 3-generation scheme (allocs, revent_allocs, prev_allocs), because by the end of GC we know number of frees for the *previous* GC. Significant caution is required to not cross yet-unknown real value of next_gc. This is achieved by 2 means: 1. Whenever I allocate a span from MCentral, I sweep a span in that MCentral. 2. Whenever I allocate N pages from MHeap, I sweep until at least N pages are returned to heap. This provides quite strong guarantees that heap does not grow when it should now. http-1 allocated 7036 7033 -0.04% allocs 60 60 +0.00% cputime 51050 46700 -8.52% gc-pause-one 34060569 1777993 -94.78% gc-pause-total 2554 133 -94.79% latency-50 178448 170926 -4.22% latency-95 284350 198294 -30.26% latency-99 345191 220652 -36.08% rss 101564416 101007360 -0.55% sys-gc 6606832 6541296 -0.99% sys-heap 88801280 87752704 -1.18% sys-other 7334208 7405928 +0.98% sys-stack 524288 524288 +0.00% sys-total 103266608 102224216 -1.01% time 50339 46533 -7.56% virtual-mem 292990976 293728256 +0.25% garbage-1 allocated 2983818 2990889 +0.24% allocs 62880 62902 +0.03% cputime 16480000 16190000 -1.76% gc-pause-one 828462467 487875135 -41.11% gc-pause-total 4142312 2439375 -41.11% rss 1151709184 1153712128 +0.17% sys-gc 66068352 66068352 +0.00% sys-heap 1039728640 1039728640 +0.00% sys-other 37776064 40770176 +7.93% sys-stack 8781824 8781824 +0.00% sys-total 1152354880 1155348992 +0.26% time 16496998 16199876 -1.80% virtual-mem 1409564672 1402281984 -0.52% LGTM=rsc R=golang-codereviews, sameer, rsc, iant, jeremyjackins, gobot CC=golang-codereviews, khr https://golang.org/cl/46430043
2014-02-12 11:16:42 -07:00
" %d/%d/%d sweeps,"
runtime: faster GC sweep phase benchmark old ns/op new ns/op delta garbage.BenchmarkParser 3731065750 3715543750 -0.41% garbage.BenchmarkParser-2 3631299750 3495248500 -3.75% garbage.BenchmarkParser-4 3386486000 3339353000 -1.39% garbage.BenchmarkParser-8 3267632000 3286422500 +0.58% garbage.BenchmarkParser-16 3299203000 3316081750 +0.51% garbage.BenchmarkTree 977532888 919453833 -5.94% garbage.BenchmarkTree-2 919948555 853478000 -7.23% garbage.BenchmarkTree-4 841329000 790207000 -6.08% garbage.BenchmarkTree-8 787792777 740380666 -6.01% garbage.BenchmarkTree-16 899257166 846594555 -5.86% garbage.BenchmarkTree2 574876300 571885800 -0.52% garbage.BenchmarkTree2-2 348162700 345888900 -0.65% garbage.BenchmarkTree2-4 184912500 179137000 -3.22% garbage.BenchmarkTree2-8 104243900 103485600 -0.73% garbage.BenchmarkTree2-16 97269500 85137100 -14.25% garbage.BenchmarkParserPause 141101976 157746974 +11.80% garbage.BenchmarkParserPause-2 103096051 83043048 -19.45% garbage.BenchmarkParserPause-4 52153133 45951111 -11.89% garbage.BenchmarkParserPause-8 36730190 38901024 +5.91% garbage.BenchmarkParserPause-16 32678875 29578585 -9.49% garbage.BenchmarkTreePause 29487065 29648439 +0.55% garbage.BenchmarkTreePause-2 22443494 21306159 -5.07% garbage.BenchmarkTreePause-4 15799691 14985647 -5.15% garbage.BenchmarkTreePause-8 10768112 9531420 -12.97% garbage.BenchmarkTreePause-16 16329891 15205158 -6.89% garbage.BenchmarkTree2Pause 2586957240 2577533200 -0.36% garbage.BenchmarkTree2Pause-2 1683383760 1673923800 -0.56% garbage.BenchmarkTree2Pause-4 1102860320 1074040280 -2.68% garbage.BenchmarkTree2Pause-8 902627920 886122400 -1.86% garbage.BenchmarkTree2Pause-16 856470920 804152320 -6.50% garbage.BenchmarkParserLastPause 277316000 280839000 +1.25% garbage.BenchmarkParserLastPause-2 179446000 163687000 -8.78% garbage.BenchmarkParserLastPause-4 106752000 94144000 -11.81% garbage.BenchmarkParserLastPause-8 57758000 61640000 +6.72% garbage.BenchmarkParserLastPause-16 51235000 42552000 -16.95% garbage.BenchmarkTreeLastPause 45244000 50786000 +12.25% garbage.BenchmarkTreeLastPause-2 37163000 34654000 -6.75% garbage.BenchmarkTreeLastPause-4 24178000 21967000 -9.14% garbage.BenchmarkTreeLastPause-8 20390000 15648000 -30.30% garbage.BenchmarkTreeLastPause-16 22398000 20180000 -9.90% garbage.BenchmarkTree2LastPause 5748706000 5718809000 -0.52% garbage.BenchmarkTree2LastPause-2 3481570000 3458844000 -0.65% garbage.BenchmarkTree2LastPause-4 1849073000 1791330000 -3.22% garbage.BenchmarkTree2LastPause-8 1042375000 1034811000 -0.73% garbage.BenchmarkTree2LastPause-16 972637000 851323000 -14.25% There is also visible improvement in consumed CPU time: tree2 -heapsize=8000000000 -cpus=12 before: 248.74user 6.36system 0:52.74elapsed 483%CPU after: 229.86user 6.33system 0:51.08elapsed 462%CPU -1.66s of real time, but -18.91s of consumed CPU time R=golang-dev CC=golang-dev https://golang.org/cl/6215065
2012-05-22 11:35:52 -06:00
" %D(%D) handoff, %D(%D) steal, %D/%D/%D yields\n",
mstats.numgc, work.nproc, (t1-t0)/1000, (t2-t1)/1000, (t3-t2)/1000, (t4-t3)/1000,
runtime: concurrent GC sweep Moves sweep phase out of stoptheworld by adding background sweeper goroutine and lazy on-demand sweeping. It turned out to be somewhat trickier than I expected, because there is no point in time when we know size of live heap nor consistent number of mallocs and frees. So everything related to next_gc, mprof, memstats, etc becomes trickier. At the end of GC next_gc is conservatively set to heap_alloc*GOGC, which is much larger than real value. But after every sweep next_gc is decremented by freed*GOGC. So when everything is swept next_gc becomes what it should be. For mprof I had to introduce 3-generation scheme (allocs, revent_allocs, prev_allocs), because by the end of GC we know number of frees for the *previous* GC. Significant caution is required to not cross yet-unknown real value of next_gc. This is achieved by 2 means: 1. Whenever I allocate a span from MCentral, I sweep a span in that MCentral. 2. Whenever I allocate N pages from MHeap, I sweep until at least N pages are returned to heap. This provides quite strong guarantees that heap does not grow when it should now. http-1 allocated 7036 7033 -0.04% allocs 60 60 +0.00% cputime 51050 46700 -8.52% gc-pause-one 34060569 1777993 -94.78% gc-pause-total 2554 133 -94.79% latency-50 178448 170926 -4.22% latency-95 284350 198294 -30.26% latency-99 345191 220652 -36.08% rss 101564416 101007360 -0.55% sys-gc 6606832 6541296 -0.99% sys-heap 88801280 87752704 -1.18% sys-other 7334208 7405928 +0.98% sys-stack 524288 524288 +0.00% sys-total 103266608 102224216 -1.01% time 50339 46533 -7.56% virtual-mem 292990976 293728256 +0.25% garbage-1 allocated 2983818 2990889 +0.24% allocs 62880 62902 +0.03% cputime 16480000 16190000 -1.76% gc-pause-one 828462467 487875135 -41.11% gc-pause-total 4142312 2439375 -41.11% rss 1151709184 1153712128 +0.17% sys-gc 66068352 66068352 +0.00% sys-heap 1039728640 1039728640 +0.00% sys-other 37776064 40770176 +7.93% sys-stack 8781824 8781824 +0.00% sys-total 1152354880 1155348992 +0.26% time 16496998 16199876 -1.80% virtual-mem 1409564672 1402281984 -0.52% LGTM=rsc R=golang-codereviews, sameer, rsc, iant, jeremyjackins, gobot CC=golang-codereviews, khr https://golang.org/cl/46430043
2014-02-12 11:16:42 -07:00
heap0>>20, heap1>>20, obj,
mstats.nmalloc, mstats.nfree,
sweep.nspan, sweep.nbgsweep, sweep.npausesweep,
runtime: make GC stats per-M This is factored out part of: https://golang.org/cl/5279048/ (Parallel GC) benchmark old ns/op new ns/op delta garbage.BenchmarkParser 3999106750 3975026500 -0.60% garbage.BenchmarkParser-2 3720553750 3719196500 -0.04% garbage.BenchmarkParser-4 3502857000 3474980500 -0.80% garbage.BenchmarkParser-8 3375448000 3341310500 -1.01% garbage.BenchmarkParserLastPause 329401000 324097000 -1.61% garbage.BenchmarkParserLastPause-2 208953000 214222000 +2.52% garbage.BenchmarkParserLastPause-4 110933000 111656000 +0.65% garbage.BenchmarkParserLastPause-8 71969000 78230000 +8.70% garbage.BenchmarkParserPause 230808842 197237400 -14.55% garbage.BenchmarkParserPause-2 123674365 125197595 +1.23% garbage.BenchmarkParserPause-4 80518525 85710333 +6.45% garbage.BenchmarkParserPause-8 58310243 56940512 -2.35% garbage.BenchmarkTree2 31471700 31289400 -0.58% garbage.BenchmarkTree2-2 21536800 21086300 -2.09% garbage.BenchmarkTree2-4 11074700 10880000 -1.76% garbage.BenchmarkTree2-8 7568600 7351400 -2.87% garbage.BenchmarkTree2LastPause 314664000 312840000 -0.58% garbage.BenchmarkTree2LastPause-2 215319000 210815000 -2.09% garbage.BenchmarkTree2LastPause-4 110698000 108751000 -1.76% garbage.BenchmarkTree2LastPause-8 75635000 73463000 -2.87% garbage.BenchmarkTree2Pause 174280857 173147571 -0.65% garbage.BenchmarkTree2Pause-2 131332714 129665761 -1.27% garbage.BenchmarkTree2Pause-4 93803095 93422904 -0.41% garbage.BenchmarkTree2Pause-8 86242333 85146761 -1.27% R=rsc CC=golang-dev https://golang.org/cl/5987045
2012-04-05 10:48:28 -06:00
stats.nhandoff, stats.nhandoffcnt,
runtime: concurrent GC sweep Moves sweep phase out of stoptheworld by adding background sweeper goroutine and lazy on-demand sweeping. It turned out to be somewhat trickier than I expected, because there is no point in time when we know size of live heap nor consistent number of mallocs and frees. So everything related to next_gc, mprof, memstats, etc becomes trickier. At the end of GC next_gc is conservatively set to heap_alloc*GOGC, which is much larger than real value. But after every sweep next_gc is decremented by freed*GOGC. So when everything is swept next_gc becomes what it should be. For mprof I had to introduce 3-generation scheme (allocs, revent_allocs, prev_allocs), because by the end of GC we know number of frees for the *previous* GC. Significant caution is required to not cross yet-unknown real value of next_gc. This is achieved by 2 means: 1. Whenever I allocate a span from MCentral, I sweep a span in that MCentral. 2. Whenever I allocate N pages from MHeap, I sweep until at least N pages are returned to heap. This provides quite strong guarantees that heap does not grow when it should now. http-1 allocated 7036 7033 -0.04% allocs 60 60 +0.00% cputime 51050 46700 -8.52% gc-pause-one 34060569 1777993 -94.78% gc-pause-total 2554 133 -94.79% latency-50 178448 170926 -4.22% latency-95 284350 198294 -30.26% latency-99 345191 220652 -36.08% rss 101564416 101007360 -0.55% sys-gc 6606832 6541296 -0.99% sys-heap 88801280 87752704 -1.18% sys-other 7334208 7405928 +0.98% sys-stack 524288 524288 +0.00% sys-total 103266608 102224216 -1.01% time 50339 46533 -7.56% virtual-mem 292990976 293728256 +0.25% garbage-1 allocated 2983818 2990889 +0.24% allocs 62880 62902 +0.03% cputime 16480000 16190000 -1.76% gc-pause-one 828462467 487875135 -41.11% gc-pause-total 4142312 2439375 -41.11% rss 1151709184 1153712128 +0.17% sys-gc 66068352 66068352 +0.00% sys-heap 1039728640 1039728640 +0.00% sys-other 37776064 40770176 +7.93% sys-stack 8781824 8781824 +0.00% sys-total 1152354880 1155348992 +0.26% time 16496998 16199876 -1.80% virtual-mem 1409564672 1402281984 -0.52% LGTM=rsc R=golang-codereviews, sameer, rsc, iant, jeremyjackins, gobot CC=golang-codereviews, khr https://golang.org/cl/46430043
2014-02-12 11:16:42 -07:00
work.markfor->nsteal, work.markfor->nstealcnt,
runtime: make GC stats per-M This is factored out part of: https://golang.org/cl/5279048/ (Parallel GC) benchmark old ns/op new ns/op delta garbage.BenchmarkParser 3999106750 3975026500 -0.60% garbage.BenchmarkParser-2 3720553750 3719196500 -0.04% garbage.BenchmarkParser-4 3502857000 3474980500 -0.80% garbage.BenchmarkParser-8 3375448000 3341310500 -1.01% garbage.BenchmarkParserLastPause 329401000 324097000 -1.61% garbage.BenchmarkParserLastPause-2 208953000 214222000 +2.52% garbage.BenchmarkParserLastPause-4 110933000 111656000 +0.65% garbage.BenchmarkParserLastPause-8 71969000 78230000 +8.70% garbage.BenchmarkParserPause 230808842 197237400 -14.55% garbage.BenchmarkParserPause-2 123674365 125197595 +1.23% garbage.BenchmarkParserPause-4 80518525 85710333 +6.45% garbage.BenchmarkParserPause-8 58310243 56940512 -2.35% garbage.BenchmarkTree2 31471700 31289400 -0.58% garbage.BenchmarkTree2-2 21536800 21086300 -2.09% garbage.BenchmarkTree2-4 11074700 10880000 -1.76% garbage.BenchmarkTree2-8 7568600 7351400 -2.87% garbage.BenchmarkTree2LastPause 314664000 312840000 -0.58% garbage.BenchmarkTree2LastPause-2 215319000 210815000 -2.09% garbage.BenchmarkTree2LastPause-4 110698000 108751000 -1.76% garbage.BenchmarkTree2LastPause-8 75635000 73463000 -2.87% garbage.BenchmarkTree2Pause 174280857 173147571 -0.65% garbage.BenchmarkTree2Pause-2 131332714 129665761 -1.27% garbage.BenchmarkTree2Pause-4 93803095 93422904 -0.41% garbage.BenchmarkTree2Pause-8 86242333 85146761 -1.27% R=rsc CC=golang-dev https://golang.org/cl/5987045
2012-04-05 10:48:28 -06:00
stats.nprocyield, stats.nosyield, stats.nsleep);
sweep.nbgsweep = sweep.npausesweep = 0;
}
runtime: faster GC sweep phase benchmark old ns/op new ns/op delta garbage.BenchmarkParser 3731065750 3715543750 -0.41% garbage.BenchmarkParser-2 3631299750 3495248500 -3.75% garbage.BenchmarkParser-4 3386486000 3339353000 -1.39% garbage.BenchmarkParser-8 3267632000 3286422500 +0.58% garbage.BenchmarkParser-16 3299203000 3316081750 +0.51% garbage.BenchmarkTree 977532888 919453833 -5.94% garbage.BenchmarkTree-2 919948555 853478000 -7.23% garbage.BenchmarkTree-4 841329000 790207000 -6.08% garbage.BenchmarkTree-8 787792777 740380666 -6.01% garbage.BenchmarkTree-16 899257166 846594555 -5.86% garbage.BenchmarkTree2 574876300 571885800 -0.52% garbage.BenchmarkTree2-2 348162700 345888900 -0.65% garbage.BenchmarkTree2-4 184912500 179137000 -3.22% garbage.BenchmarkTree2-8 104243900 103485600 -0.73% garbage.BenchmarkTree2-16 97269500 85137100 -14.25% garbage.BenchmarkParserPause 141101976 157746974 +11.80% garbage.BenchmarkParserPause-2 103096051 83043048 -19.45% garbage.BenchmarkParserPause-4 52153133 45951111 -11.89% garbage.BenchmarkParserPause-8 36730190 38901024 +5.91% garbage.BenchmarkParserPause-16 32678875 29578585 -9.49% garbage.BenchmarkTreePause 29487065 29648439 +0.55% garbage.BenchmarkTreePause-2 22443494 21306159 -5.07% garbage.BenchmarkTreePause-4 15799691 14985647 -5.15% garbage.BenchmarkTreePause-8 10768112 9531420 -12.97% garbage.BenchmarkTreePause-16 16329891 15205158 -6.89% garbage.BenchmarkTree2Pause 2586957240 2577533200 -0.36% garbage.BenchmarkTree2Pause-2 1683383760 1673923800 -0.56% garbage.BenchmarkTree2Pause-4 1102860320 1074040280 -2.68% garbage.BenchmarkTree2Pause-8 902627920 886122400 -1.86% garbage.BenchmarkTree2Pause-16 856470920 804152320 -6.50% garbage.BenchmarkParserLastPause 277316000 280839000 +1.25% garbage.BenchmarkParserLastPause-2 179446000 163687000 -8.78% garbage.BenchmarkParserLastPause-4 106752000 94144000 -11.81% garbage.BenchmarkParserLastPause-8 57758000 61640000 +6.72% garbage.BenchmarkParserLastPause-16 51235000 42552000 -16.95% garbage.BenchmarkTreeLastPause 45244000 50786000 +12.25% garbage.BenchmarkTreeLastPause-2 37163000 34654000 -6.75% garbage.BenchmarkTreeLastPause-4 24178000 21967000 -9.14% garbage.BenchmarkTreeLastPause-8 20390000 15648000 -30.30% garbage.BenchmarkTreeLastPause-16 22398000 20180000 -9.90% garbage.BenchmarkTree2LastPause 5748706000 5718809000 -0.52% garbage.BenchmarkTree2LastPause-2 3481570000 3458844000 -0.65% garbage.BenchmarkTree2LastPause-4 1849073000 1791330000 -3.22% garbage.BenchmarkTree2LastPause-8 1042375000 1034811000 -0.73% garbage.BenchmarkTree2LastPause-16 972637000 851323000 -14.25% There is also visible improvement in consumed CPU time: tree2 -heapsize=8000000000 -cpus=12 before: 248.74user 6.36system 0:52.74elapsed 483%CPU after: 229.86user 6.33system 0:51.08elapsed 462%CPU -1.66s of real time, but -18.91s of consumed CPU time R=golang-dev CC=golang-dev https://golang.org/cl/6215065
2012-05-22 11:35:52 -06:00
runtime: concurrent GC sweep Moves sweep phase out of stoptheworld by adding background sweeper goroutine and lazy on-demand sweeping. It turned out to be somewhat trickier than I expected, because there is no point in time when we know size of live heap nor consistent number of mallocs and frees. So everything related to next_gc, mprof, memstats, etc becomes trickier. At the end of GC next_gc is conservatively set to heap_alloc*GOGC, which is much larger than real value. But after every sweep next_gc is decremented by freed*GOGC. So when everything is swept next_gc becomes what it should be. For mprof I had to introduce 3-generation scheme (allocs, revent_allocs, prev_allocs), because by the end of GC we know number of frees for the *previous* GC. Significant caution is required to not cross yet-unknown real value of next_gc. This is achieved by 2 means: 1. Whenever I allocate a span from MCentral, I sweep a span in that MCentral. 2. Whenever I allocate N pages from MHeap, I sweep until at least N pages are returned to heap. This provides quite strong guarantees that heap does not grow when it should now. http-1 allocated 7036 7033 -0.04% allocs 60 60 +0.00% cputime 51050 46700 -8.52% gc-pause-one 34060569 1777993 -94.78% gc-pause-total 2554 133 -94.79% latency-50 178448 170926 -4.22% latency-95 284350 198294 -30.26% latency-99 345191 220652 -36.08% rss 101564416 101007360 -0.55% sys-gc 6606832 6541296 -0.99% sys-heap 88801280 87752704 -1.18% sys-other 7334208 7405928 +0.98% sys-stack 524288 524288 +0.00% sys-total 103266608 102224216 -1.01% time 50339 46533 -7.56% virtual-mem 292990976 293728256 +0.25% garbage-1 allocated 2983818 2990889 +0.24% allocs 62880 62902 +0.03% cputime 16480000 16190000 -1.76% gc-pause-one 828462467 487875135 -41.11% gc-pause-total 4142312 2439375 -41.11% rss 1151709184 1153712128 +0.17% sys-gc 66068352 66068352 +0.00% sys-heap 1039728640 1039728640 +0.00% sys-other 37776064 40770176 +7.93% sys-stack 8781824 8781824 +0.00% sys-total 1152354880 1155348992 +0.26% time 16496998 16199876 -1.80% virtual-mem 1409564672 1402281984 -0.52% LGTM=rsc R=golang-codereviews, sameer, rsc, iant, jeremyjackins, gobot CC=golang-codereviews, khr https://golang.org/cl/46430043
2014-02-12 11:16:42 -07:00
// We cache current runtime·mheap.allspans array in sweep.spans,
// because the former can be resized and freed.
// Otherwise we would need to take heap lock every time
// we want to convert span index to span pointer.
// Free the old cached array if necessary.
if(sweep.spans && sweep.spans != runtime·mheap.allspans)
runtime·SysFree(sweep.spans, sweep.nspan*sizeof(sweep.spans[0]), &mstats.other_sys);
// Cache the current array.
runtime·mheap.sweepspans = runtime·mheap.allspans;
runtime·mheap.sweepgen += 2;
runtime·mheap.sweepdone = false;
sweep.spans = runtime·mheap.allspans;
sweep.nspan = runtime·mheap.nspan;
sweep.spanidx = 0;
// Temporary disable concurrent sweep, because we see failures on builders.
if(ConcurrentSweep && !args->eagersweep) {
runtime·lock(&gclock);
if(sweep.g == nil)
sweep.g = runtime·newproc1(&bgsweepv, nil, 0, 0, runtime·gc);
else if(sweep.parked) {
sweep.parked = false;
runtime·ready(sweep.g);
}
runtime·unlock(&gclock);
} else {
// Sweep all spans eagerly.
while(runtime·sweepone() != -1)
sweep.npausesweep++;
runtime: concurrent GC sweep Moves sweep phase out of stoptheworld by adding background sweeper goroutine and lazy on-demand sweeping. It turned out to be somewhat trickier than I expected, because there is no point in time when we know size of live heap nor consistent number of mallocs and frees. So everything related to next_gc, mprof, memstats, etc becomes trickier. At the end of GC next_gc is conservatively set to heap_alloc*GOGC, which is much larger than real value. But after every sweep next_gc is decremented by freed*GOGC. So when everything is swept next_gc becomes what it should be. For mprof I had to introduce 3-generation scheme (allocs, revent_allocs, prev_allocs), because by the end of GC we know number of frees for the *previous* GC. Significant caution is required to not cross yet-unknown real value of next_gc. This is achieved by 2 means: 1. Whenever I allocate a span from MCentral, I sweep a span in that MCentral. 2. Whenever I allocate N pages from MHeap, I sweep until at least N pages are returned to heap. This provides quite strong guarantees that heap does not grow when it should now. http-1 allocated 7036 7033 -0.04% allocs 60 60 +0.00% cputime 51050 46700 -8.52% gc-pause-one 34060569 1777993 -94.78% gc-pause-total 2554 133 -94.79% latency-50 178448 170926 -4.22% latency-95 284350 198294 -30.26% latency-99 345191 220652 -36.08% rss 101564416 101007360 -0.55% sys-gc 6606832 6541296 -0.99% sys-heap 88801280 87752704 -1.18% sys-other 7334208 7405928 +0.98% sys-stack 524288 524288 +0.00% sys-total 103266608 102224216 -1.01% time 50339 46533 -7.56% virtual-mem 292990976 293728256 +0.25% garbage-1 allocated 2983818 2990889 +0.24% allocs 62880 62902 +0.03% cputime 16480000 16190000 -1.76% gc-pause-one 828462467 487875135 -41.11% gc-pause-total 4142312 2439375 -41.11% rss 1151709184 1153712128 +0.17% sys-gc 66068352 66068352 +0.00% sys-heap 1039728640 1039728640 +0.00% sys-other 37776064 40770176 +7.93% sys-stack 8781824 8781824 +0.00% sys-total 1152354880 1155348992 +0.26% time 16496998 16199876 -1.80% virtual-mem 1409564672 1402281984 -0.52% LGTM=rsc R=golang-codereviews, sameer, rsc, iant, jeremyjackins, gobot CC=golang-codereviews, khr https://golang.org/cl/46430043
2014-02-12 11:16:42 -07:00
}
runtime·MProf_GC();
2014-06-26 09:54:39 -06:00
g->m->traceback = 0;
}
2014-01-30 02:28:19 -07:00
extern uintptr runtime·sizeof_C_MStats;
void
runtime·ReadMemStats(MStats *stats)
{
// Have to acquire worldsema to stop the world,
// because stoptheworld can only be used by
// one goroutine at a time, and there might be
// a pending garbage collection already calling it.
runtime·semacquire(&runtime·worldsema, false);
2014-06-26 09:54:39 -06:00
g->m->gcing = 1;
runtime·stoptheworld();
runtime·updatememstats(nil);
2014-01-30 02:28:19 -07:00
// Size of the trailing by_size array differs between Go and C,
// NumSizeClasses was changed, but we can not change Go struct because of backward compatibility.
runtime·memmove(stats, &mstats, runtime·sizeof_C_MStats);
// Stack numbers are part of the heap numbers, separate those out for user consumption
stats->stacks_sys = stats->stacks_inuse;
stats->heap_inuse -= stats->stacks_inuse;
stats->heap_sys -= stats->stacks_inuse;
2014-06-26 09:54:39 -06:00
g->m->gcing = 0;
g->m->locks++;
runtime·semrelease(&runtime·worldsema);
runtime·starttheworld();
2014-06-26 09:54:39 -06:00
g->m->locks--;
}
void
runtimedebug·readGCStats(Slice *pauses)
{
uint64 *p;
uint32 i, n;
// Calling code in runtime/debug should make the slice large enough.
if(pauses->cap < nelem(mstats.pause_ns)+3)
runtime·throw("runtime: short slice passed to readGCStats");
// Pass back: pauses, last gc (absolute time), number of gc, total pause ns.
p = (uint64*)pauses->array;
runtime·lock(&runtime·mheap.lock);
n = mstats.numgc;
if(n > nelem(mstats.pause_ns))
n = nelem(mstats.pause_ns);
// The pause buffer is circular. The most recent pause is at
// pause_ns[(numgc-1)%nelem(pause_ns)], and then backward
// from there to go back farther in time. We deliver the times
// most recent first (in p[0]).
for(i=0; i<n; i++)
p[i] = mstats.pause_ns[(mstats.numgc-1-i)%nelem(mstats.pause_ns)];
p[n] = mstats.last_gc;
p[n+1] = mstats.numgc;
p[n+2] = mstats.pause_total_ns;
runtime·unlock(&runtime·mheap.lock);
pauses->len = n+3;
}
runtime: use goc2c as much as possible Package runtime's C functions written to be called from Go started out written in C using carefully constructed argument lists and the FLUSH macro to write a result back to memory. For some functions, the appropriate parameter list ended up being architecture-dependent due to differences in alignment, so we added 'goc2c', which takes a .goc file containing Go func declarations but C bodies, rewrites the Go func declaration to equivalent C declarations for the target architecture, adds the needed FLUSH statements, and writes out an equivalent C file. That C file is compiled as part of package runtime. Native Client's x86-64 support introduces the most complex alignment rules yet, breaking many functions that could until now be portably written in C. Using goc2c for those avoids the breakage. Separately, Keith's work on emitting stack information from the C compiler would require the hand-written functions to add #pragmas specifying how many arguments are result parameters. Using goc2c for those avoids maintaining #pragmas. For both reasons, use goc2c for as many Go-called C functions as possible. This CL is a replay of the bulk of CL 15400047 and CL 15790043, both of which were reviewed as part of the NaCl port and are checked in to the NaCl branch. This CL is part of bringing the NaCl code into the main tree. No new code here, just reformatting and occasional movement into .h files. LGTM=r R=dave, alex.brainman, r CC=golang-codereviews https://golang.org/cl/65220044
2014-02-20 13:58:47 -07:00
int32
runtime·setgcpercent(int32 in) {
int32 out;
runtime·lock(&runtime·mheap.lock);
out = runtime·gcpercent;
if(in < 0)
in = -1;
runtime·gcpercent = in;
runtime·unlock(&runtime·mheap.lock);
runtime: use goc2c as much as possible Package runtime's C functions written to be called from Go started out written in C using carefully constructed argument lists and the FLUSH macro to write a result back to memory. For some functions, the appropriate parameter list ended up being architecture-dependent due to differences in alignment, so we added 'goc2c', which takes a .goc file containing Go func declarations but C bodies, rewrites the Go func declaration to equivalent C declarations for the target architecture, adds the needed FLUSH statements, and writes out an equivalent C file. That C file is compiled as part of package runtime. Native Client's x86-64 support introduces the most complex alignment rules yet, breaking many functions that could until now be portably written in C. Using goc2c for those avoids the breakage. Separately, Keith's work on emitting stack information from the C compiler would require the hand-written functions to add #pragmas specifying how many arguments are result parameters. Using goc2c for those avoids maintaining #pragmas. For both reasons, use goc2c for as many Go-called C functions as possible. This CL is a replay of the bulk of CL 15400047 and CL 15790043, both of which were reviewed as part of the NaCl port and are checked in to the NaCl branch. This CL is part of bringing the NaCl code into the main tree. No new code here, just reformatting and occasional movement into .h files. LGTM=r R=dave, alex.brainman, r CC=golang-codereviews https://golang.org/cl/65220044
2014-02-20 13:58:47 -07:00
return out;
}
static void
gchelperstart(void)
{
2014-06-26 09:54:39 -06:00
if(g->m->helpgc < 0 || g->m->helpgc >= MaxGcproc)
runtime·throw("gchelperstart: bad m->helpgc");
2014-06-26 09:54:39 -06:00
if(g != g->m->g0)
runtime·throw("gchelper not running on g0 stack");
}
static void
runfinq(void)
{
Finalizer *f;
FinBlock *fb, *next;
byte *frame;
uint32 framesz, framecap, i;
Eface *ef, ef1;
// This function blocks for long periods of time, and because it is written in C
// we have no liveness information. Zero everything so that uninitialized pointers
// do not cause memory leaks.
f = nil;
fb = nil;
next = nil;
frame = nil;
framecap = 0;
framesz = 0;
i = 0;
ef = nil;
ef1.type = nil;
ef1.data = nil;
// force flush to memory
USED(&f);
USED(&fb);
USED(&next);
USED(&framesz);
USED(&i);
USED(&ef);
USED(&ef1);
for(;;) {
runtime·lock(&finlock);
fb = finq;
finq = nil;
if(fb == nil) {
runtime·fingwait = true;
g->isbackground = true;
runtime·parkunlock(&finlock, "finalizer wait");
g->isbackground = false;
continue;
}
runtime·unlock(&finlock);
if(raceenabled)
runtime·racefingo();
for(; fb; fb=next) {
next = fb->next;
for(i=0; i<fb->cnt; i++) {
f = &fb->fin[i];
framesz = sizeof(Eface) + f->nret;
if(framecap < framesz) {
// The frame does not contain pointers interesting for GC,
// all not yet finalized objects are stored in finq.
// If we do not mark it as FlagNoScan,
// the last finalized object is not collected.
frame = runtime·mallocgc(framesz, 0, FlagNoScan);
framecap = framesz;
}
if(f->fint == nil)
runtime·throw("missing type in runfinq");
if((f->fint->kind&KindMask) == KindPtr) {
// direct use of pointer
*(void**)frame = f->arg;
} else if(((InterfaceType*)f->fint)->mhdr.len == 0) {
// convert to empty interface
ef = (Eface*)frame;
ef->type = &f->ot->typ;
ef->data = f->arg;
} else {
// convert to interface with methods, via empty interface.
ef1.type = &f->ot->typ;
ef1.data = f->arg;
if(!runtime·ifaceE2I2((InterfaceType*)f->fint, ef1, (Iface*)frame))
runtime·throw("invalid type conversion in runfinq");
}
reflect, runtime: fix crash in GC due to reflect.call + precise GC Given type Outer struct { *Inner ... } the compiler generates the implementation of (*Outer).M dispatching to the embedded Inner. The implementation is logically: func (p *Outer) M() { (p.Inner).M() } but since the only change here is the replacement of one pointer receiver with another, the actual generated code overwrites the original receiver with the p.Inner pointer and then jumps to the M method expecting the *Inner receiver. During reflect.Value.Call, we create an argument frame and the associated data structures to describe it to the garbage collector, populate the frame, call reflect.call to run a function call using that frame, and then copy the results back out of the frame. The reflect.call function does a memmove of the frame structure onto the stack (to set up the inputs), runs the call, and the memmoves the stack back to the frame structure (to preserve the outputs). Originally reflect.call did not distinguish inputs from outputs: both memmoves were for the full stack frame. However, in the case where the called function was one of these wrappers, the rewritten receiver is almost certainly a different type than the original receiver. This is not a problem on the stack, where we use the program counter to determine the type information and understand that during (*Outer).M the receiver is an *Outer while during (*Inner).M the receiver in the same memory word is now an *Inner. But in the statically typed argument frame created by reflect, the receiver is always an *Outer. Copying the modified receiver pointer off the stack into the frame will store an *Inner there, and then if a garbage collection happens to scan that argument frame before it is discarded, it will scan the *Inner memory as if it were an *Outer. If the two have different memory layouts, the collection will intepret the memory incorrectly. Fix by only copying back the results. Fixes #7725. LGTM=khr R=khr CC=dave, golang-codereviews https://golang.org/cl/85180043
2014-04-08 09:11:35 -06:00
reflect·call(f->fn, frame, framesz, framesz);
f->fn = nil;
f->arg = nil;
f->ot = nil;
}
fb->cnt = 0;
runtime·lock(&finlock);
fb->next = finc;
finc = fb;
runtime·unlock(&finlock);
}
// Zero everything that's dead, to avoid memory leaks.
// See comment at top of function.
f = nil;
fb = nil;
next = nil;
i = 0;
ef = nil;
ef1.type = nil;
ef1.data = nil;
runtime·gc(1); // trigger another gc to clean up the finalized objects, if possible
}
}
void
runtime·createfing(void)
{
if(runtime·fing != nil)
return;
// Here we use gclock instead of finlock,
// because newproc1 can allocate, which can cause on-demand span sweep,
// which can queue finalizers, which would deadlock.
runtime·lock(&gclock);
if(runtime·fing == nil)
runtime·fing = runtime·newproc1(&runfinqv, nil, 0, 0, runtime·gc);
runtime·unlock(&gclock);
}
void
runtime·createfingM(G *gp)
{
runtime·createfing();
runtime·gogo(&gp->sched);
}
G*
runtime·wakefing(void)
{
G *res;
res = nil;
runtime·lock(&finlock);
if(runtime·fingwait && runtime·fingwake) {
runtime·fingwait = false;
runtime·fingwake = false;
res = runtime·fing;
}
runtime·unlock(&finlock);
return res;
}
// Recursively unrolls GC program in prog.
// mask is where to store the result.
// ppos is a pointer to position in mask, in bits.
// sparse says to generate 4-bits per word mask for heap (2-bits for data/bss otherwise).
static byte*
unrollgcprog1(byte *mask, byte *prog, uintptr *ppos, bool inplace, bool sparse)
{
uintptr pos, siz, i, off;
byte *arena_start, *prog1, v, *bitp, shift;
arena_start = runtime·mheap.arena_start;
pos = *ppos;
for(;;) {
switch(prog[0]) {
case insData:
prog++;
siz = prog[0];
prog++;
for(i = 0; i < siz; i++) {
v = prog[i/PointersPerByte];
v >>= (i%PointersPerByte)*BitsPerPointer;
v &= BitsMask;
if(inplace) {
// Store directly into GC bitmap.
off = (uintptr*)(mask+pos) - (uintptr*)arena_start;
bitp = arena_start - off/wordsPerBitmapByte - 1;
shift = (off % wordsPerBitmapByte) * gcBits;
if(shift==0)
*bitp = 0;
*bitp |= v<<(shift+2);
pos += PtrSize;
} else if(sparse) {
// 4-bits per word
v <<= (pos%8)+2;
mask[pos/8] |= v;
pos += gcBits;
} else {
// 2-bits per word
v <<= pos%8;
mask[pos/8] |= v;
pos += BitsPerPointer;
}
}
prog += ROUND(siz*BitsPerPointer, 8)/8;
break;
case insArray:
prog++;
siz = 0;
for(i = 0; i < PtrSize; i++)
siz = (siz<<8) + prog[PtrSize-i-1];
prog += PtrSize;
prog1 = nil;
for(i = 0; i < siz; i++)
prog1 = unrollgcprog1(mask, prog, &pos, inplace, sparse);
if(prog1[0] != insArrayEnd)
runtime·throw("unrollgcprog: array does not end with insArrayEnd");
prog = prog1+1;
break;
case insArrayEnd:
case insEnd:
*ppos = pos;
return prog;
default:
runtime·throw("unrollgcprog: unknown instruction");
}
}
}
// Unrolls GC program prog for data/bss, returns dense GC mask.
static byte*
unrollglobgcprog(byte *prog, uintptr size)
{
byte *mask;
uintptr pos, masksize;
masksize = ROUND(ROUND(size, PtrSize)/PtrSize*BitsPerPointer, 8)/8;
mask = runtime·persistentalloc(masksize+1, 0, &mstats.gc_sys);
mask[masksize] = 0xa1;
pos = 0;
prog = unrollgcprog1(mask, prog, &pos, false, false);
if(pos != size/PtrSize*BitsPerPointer) {
runtime·printf("unrollglobgcprog: bad program size, got %D, expect %D\n",
(uint64)pos, (uint64)size/PtrSize*BitsPerPointer);
runtime·throw("unrollglobgcprog: bad program size");
}
if(prog[0] != insEnd)
runtime·throw("unrollglobgcprog: program does not end with insEnd");
if(mask[masksize] != 0xa1)
runtime·throw("unrollglobgcprog: overflow");
return mask;
}
void
runtime·unrollgcproginplace_m(void)
{
uintptr size, size0, pos, off;
byte *arena_start, *prog, *bitp, shift;
Type *typ;
void *v;
v = g->m->ptrarg[0];
typ = g->m->ptrarg[1];
size = g->m->scalararg[0];
size0 = g->m->scalararg[1];
g->m->ptrarg[0] = nil;
g->m->ptrarg[1] = nil;
pos = 0;
prog = (byte*)typ->gc[1];
while(pos != size0)
unrollgcprog1(v, prog, &pos, true, true);
// Mark first word as bitAllocated.
arena_start = runtime·mheap.arena_start;
off = (uintptr*)v - (uintptr*)arena_start;
bitp = arena_start - off/wordsPerBitmapByte - 1;
shift = (off % wordsPerBitmapByte) * gcBits;
*bitp |= bitBoundary<<shift;
// Mark word after last as BitsDead.
if(size0 < size) {
off = (uintptr*)((byte*)v + size0) - (uintptr*)arena_start;
bitp = arena_start - off/wordsPerBitmapByte - 1;
shift = (off % wordsPerBitmapByte) * gcBits;
*bitp &= ~(bitPtrMask<<shift) | ((uintptr)BitsDead<<(shift+2));
}
}
// Unrolls GC program in typ->gc[1] into typ->gc[0]
void
runtime·unrollgcprog_m(void)
{
static Lock lock;
Type *typ;
byte *mask, *prog;
uintptr pos;
uint32 x;
typ = g->m->ptrarg[0];
g->m->ptrarg[0] = nil;
runtime·lock(&lock);
mask = (byte*)typ->gc[0];
if(mask[0] == 0) {
pos = 8; // skip the unroll flag
prog = (byte*)typ->gc[1];
prog = unrollgcprog1(mask, prog, &pos, false, true);
if(prog[0] != insEnd)
runtime·throw("unrollgcprog: program does not end with insEnd");
if(((typ->size/PtrSize)%2) != 0) {
// repeat the program twice
prog = (byte*)typ->gc[1];
unrollgcprog1(mask, prog, &pos, false, true);
}
// atomic way to say mask[0] = 1
x = ((uint32*)mask)[0];
runtime·atomicstore((uint32*)mask, x|1);
}
runtime·unlock(&lock);
}
// mark the span of memory at v as having n blocks of the given size.
// if leftover is true, there is left over space at the end of the span.
void
runtime·markspan(void *v, uintptr size, uintptr n, bool leftover)
{
uintptr i, off, step;
byte *b;
if((byte*)v+size*n > (byte*)runtime·mheap.arena_used || (byte*)v < runtime·mheap.arena_start)
runtime·throw("markspan: bad pointer");
// Find bits of the beginning of the span.
off = (uintptr*)v - (uintptr*)runtime·mheap.arena_start; // word offset
b = runtime·mheap.arena_start - off/wordsPerBitmapByte - 1;
if((off%wordsPerBitmapByte) != 0)
runtime·throw("markspan: unaligned length");
// Okay to use non-atomic ops here, because we control
// the entire span, and each bitmap byte has bits for only
// one span, so no other goroutines are changing these bitmap words.
if(size == PtrSize) {
// Possible only on 64-bits (minimal size class is 8 bytes).
// Poor man's memset(0x11).
if(0x11 != ((bitBoundary+BitsDead)<<gcBits) + (bitBoundary+BitsDead))
runtime·throw("markspan: bad bits");
if((n%(wordsPerBitmapByte*PtrSize)) != 0)
runtime·throw("markspan: unaligned length");
b = b - n/wordsPerBitmapByte + 1; // find first byte
if(((uintptr)b%PtrSize) != 0)
runtime·throw("markspan: unaligned pointer");
for(i = 0; i != n; i += wordsPerBitmapByte*PtrSize, b += PtrSize)
*(uintptr*)b = (uintptr)0x1111111111111111ULL; // bitBoundary+BitsDead
return;
}
if(leftover)
n++; // mark a boundary just past end of last block too
step = size/(PtrSize*wordsPerBitmapByte);
for(i = 0; i != n; i++, b -= step)
*b = bitBoundary|(BitsDead<<2);
}
// unmark the span of memory at v of length n bytes.
void
runtime·unmarkspan(void *v, uintptr n)
{
uintptr off;
byte *b;
if((byte*)v+n > (byte*)runtime·mheap.arena_used || (byte*)v < runtime·mheap.arena_start)
runtime·throw("markspan: bad pointer");
off = (uintptr*)v - (uintptr*)runtime·mheap.arena_start; // word offset
if((off % (PtrSize*wordsPerBitmapByte)) != 0)
runtime·throw("markspan: unaligned pointer");
b = runtime·mheap.arena_start - off/wordsPerBitmapByte - 1;
n /= PtrSize;
if(n%(PtrSize*wordsPerBitmapByte) != 0)
runtime·throw("unmarkspan: unaligned length");
// Okay to use non-atomic ops here, because we control
// the entire span, and each bitmap word has bits for only
// one span, so no other goroutines are changing these
// bitmap words.
n /= wordsPerBitmapByte;
runtime·memclr(b - n + 1, n);
}
void
runtime·MHeap_MapBits(MHeap *h)
{
// Caller has added extra mappings to the arena.
// Add extra mappings of bitmap words as needed.
// We allocate extra bitmap pieces in chunks of bitmapChunk.
enum {
bitmapChunk = 8192
};
uintptr n;
n = (h->arena_used - h->arena_start) / (PtrSize*wordsPerBitmapByte);
n = ROUND(n, bitmapChunk);
n = ROUND(n, PhysPageSize);
if(h->bitmap_mapped >= n)
return;
runtime·SysMap(h->arena_start - n, n - h->bitmap_mapped, h->arena_reserved, &mstats.gc_sys);
h->bitmap_mapped = n;
}
static bool
getgcmaskcb(Stkframe *frame, void *ctxt)
{
Stkframe *frame0;
frame0 = ctxt;
if(frame0->sp >= (uintptr)frame->varp - frame->sp && frame0->sp < (uintptr)frame->varp) {
*frame0 = *frame;
return false;
}
return true;
}
// Returns GC type info for object p for testing.
void
runtime·getgcmask(byte *p, Type *t, byte **mask, uintptr *len)
{
Stkframe frame;
uintptr i, n, off;
byte *base, bits, shift, *b;
*mask = nil;
*len = 0;
// data
if(p >= data && p < edata) {
n = ((PtrType*)t)->elem->size;
*len = n/PtrSize;
*mask = runtime·mallocgc(*len, nil, 0);
for(i = 0; i < n; i += PtrSize) {
off = (p+i-data)/PtrSize;
bits = (runtime·gcdatamask[off/PointersPerByte] >> ((off%PointersPerByte)*BitsPerPointer))&BitsMask;
(*mask)[i/PtrSize] = bits;
}
return;
}
// bss
if(p >= bss && p < ebss) {
n = ((PtrType*)t)->elem->size;
*len = n/PtrSize;
*mask = runtime·mallocgc(*len, nil, 0);
for(i = 0; i < n; i += PtrSize) {
off = (p+i-bss)/PtrSize;
bits = (runtime·gcbssmask[off/PointersPerByte] >> ((off%PointersPerByte)*BitsPerPointer))&BitsMask;
(*mask)[i/PtrSize] = bits;
}
return;
}
// heap
if(runtime·mlookup(p, &base, &n, nil)) {
*len = n/PtrSize;
*mask = runtime·mallocgc(*len, nil, 0);
for(i = 0; i < n; i += PtrSize) {
off = (uintptr*)(base+i) - (uintptr*)runtime·mheap.arena_start;
b = runtime·mheap.arena_start - off/wordsPerBitmapByte - 1;
shift = (off % wordsPerBitmapByte) * gcBits;
bits = (*b >> (shift+2))&BitsMask;
(*mask)[i/PtrSize] = bits;
}
return;
}
// stack
frame.fn = nil;
frame.sp = (uintptr)p;
runtime·gentraceback((uintptr)runtime·getcallerpc(&p), (uintptr)runtime·getcallersp(&p), 0, g, 0, nil, 1000, getgcmaskcb, &frame, false);
if(frame.fn != nil) {
Func *f;
StackMap *stackmap;
BitVector bv;
uintptr size;
uintptr targetpc;
int32 pcdata;
f = frame.fn;
targetpc = frame.continpc;
if(targetpc == 0)
return;
if(targetpc != f->entry)
targetpc--;
pcdata = runtime·pcdatavalue(f, PCDATA_StackMapIndex, targetpc);
if(pcdata == -1)
return;
stackmap = runtime·funcdata(f, FUNCDATA_LocalsPointerMaps);
if(stackmap == nil || stackmap->n <= 0)
return;
bv = runtime·stackmapdata(stackmap, pcdata);
size = bv.n/BitsPerPointer*PtrSize;
n = ((PtrType*)t)->elem->size;
*len = n/PtrSize;
*mask = runtime·mallocgc(*len, nil, 0);
for(i = 0; i < n; i += PtrSize) {
off = (p+i-frame.varp+size)/PtrSize;
bits = (bv.data[off*BitsPerPointer/32] >> ((off*BitsPerPointer)%32))&BitsMask;
(*mask)[i/PtrSize] = bits;
}
}
}