Commit 51b8edcb authored by Jan Ziak's avatar Jan Ziak Committed by Russ Cox

runtime: use reflect·call() to enter the function gc()

Garbage collection code (to be merged later) is calling functions
which have many local variables. This increases the probability that
the stack capacity won't be big enough to hold the local variables.
So, start gc() on a bigger stack to eliminate a potentially large number
of calls to runtime·morestack().

R=rsc, remyoudompheng, dsymonds, minux.ma, iant, iant
CC=golang-dev
https://golang.org/cl/6846044
parent 58ce93b6
......@@ -881,6 +881,7 @@ runtime·memorydump(void)
dumpspan(spanidx);
}
}
void
runtime·gchelper(void)
{
......@@ -957,15 +958,20 @@ cachestats(GCStats *stats)
mstats.stacks_sys = stacks_sys;
}
// Structure of arguments passed to function gc().
// This allows the arguments to be passed via reflect·call.
struct gc_args
{
int32 force;
};
static void gc(struct gc_args *args);
void
runtime·gc(int32 force)
{
int64 t0, t1, t2, t3;
uint64 heap0, heap1, obj0, obj1;
byte *p;
GCStats stats;
M *m1;
uint32 i;
struct gc_args a, *ap;
// The atomic operations are not atomic if the uint64s
// are not aligned on uint64 boundaries. This has been
......@@ -1000,8 +1006,30 @@ runtime·gc(int32 force)
if(gcpercent < 0)
return;
// Run gc on a bigger stack to eliminate
// a potentially large number of calls to runtime·morestack.
a.force = force;
ap = &a;
m->moreframesize_minalloc = StackBig;
reflect·call((byte*)gc, (byte*)&ap, sizeof(ap));
if(gctrace > 1 && !force) {
a.force = 1;
gc(&a);
}
}
static void
gc(struct gc_args *args)
{
int64 t0, t1, t2, t3;
uint64 heap0, heap1, obj0, obj1;
GCStats stats;
M *m1;
uint32 i;
runtime·semacquire(&runtime·worldsema);
if(!force && mstats.heap_alloc < mstats.next_gc) {
if(!args->force && mstats.heap_alloc < mstats.next_gc) {
runtime·semrelease(&runtime·worldsema);
return;
}
......@@ -1107,9 +1135,6 @@ runtime·gc(int32 force)
// give the queued finalizers, if any, a chance to run
if(finq != nil)
runtime·gosched();
if(gctrace > 1 && !force)
runtime·gc(1);
}
void
......
......@@ -1134,7 +1134,7 @@ runtime·oldstack(void)
void
runtime·newstack(void)
{
int32 framesize, argsize;
int32 framesize, minalloc, argsize;
Stktop *top;
byte *stk, *sp;
G *g1;
......@@ -1143,9 +1143,12 @@ runtime·newstack(void)
uintptr free;
framesize = m->moreframesize;
minalloc = m->moreframesize_minalloc;
argsize = m->moreargsize;
g1 = m->curg;
m->moreframesize_minalloc = 0;
if(m->morebuf.sp < g1->stackguard - StackGuard) {
runtime·printf("runtime: split stack overflow: %p < %p\n", m->morebuf.sp, g1->stackguard - StackGuard);
runtime·throw("runtime: split stack overflow");
......@@ -1159,7 +1162,10 @@ runtime·newstack(void)
if(reflectcall)
framesize = 0;
if(reflectcall && m->morebuf.sp - sizeof(Stktop) - argsize - 32 > g1->stackguard) {
if(framesize < minalloc)
framesize = minalloc;
if(reflectcall && minalloc == 0 && m->morebuf.sp - sizeof(Stktop) - argsize - 32 > g1->stackguard) {
// special case: called from reflect.call (framesize==1)
// to call code with an arbitrary argument size,
// and we have enough space on the current stack.
......@@ -1180,8 +1186,10 @@ runtime·newstack(void)
free = framesize;
}
//runtime·printf("newstack framesize=%d argsize=%d morepc=%p moreargp=%p gobuf=%p, %p top=%p old=%p\n",
//framesize, argsize, m->morepc, m->moreargp, m->morebuf.pc, m->morebuf.sp, top, g1->stackbase);
if(0) {
runtime·printf("newstack framesize=%d argsize=%d morepc=%p moreargp=%p gobuf=%p, %p top=%p old=%p\n",
framesize, argsize, m->morepc, m->moreargp, m->morebuf.pc, m->morebuf.sp, top, g1->stackbase);
}
top->stackbase = (byte*)g1->stackbase;
top->stackguard = (byte*)g1->stackguard;
......
......@@ -273,6 +273,7 @@ struct M
GCStats gcstats;
bool racecall;
void* racepc;
uint32 moreframesize_minalloc;
uintptr settype_buf[1024];
uintptr settype_bufsize;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment