Commit 1d7faf91 authored by Dmitriy Vyukov's avatar Dmitriy Vyukov

runtime: minor changes

to minimize diffs of new scheduler

R=golang-dev, rsc
CC=golang-dev
https://golang.org/cl/7381048
parent 4434212f
...@@ -239,6 +239,8 @@ runtime·main(void) ...@@ -239,6 +239,8 @@ runtime·main(void)
// by calling runtime.LockOSThread during initialization // by calling runtime.LockOSThread during initialization
// to preserve the lock. // to preserve the lock.
runtime·lockOSThread(); runtime·lockOSThread();
if(m != &runtime·m0)
runtime·throw("runtime·main not on m0");
// From now on, newgoroutines may use non-main threads. // From now on, newgoroutines may use non-main threads.
setmcpumax(runtime·gomaxprocs); setmcpumax(runtime·gomaxprocs);
runtime·sched.init = true; runtime·sched.init = true;
...@@ -672,6 +674,7 @@ runtime·gcprocs(void) ...@@ -672,6 +674,7 @@ runtime·gcprocs(void)
// Figure out how many CPUs to use during GC. // Figure out how many CPUs to use during GC.
// Limited by gomaxprocs, number of actual CPUs, and MaxGcproc. // Limited by gomaxprocs, number of actual CPUs, and MaxGcproc.
runtime·lock(&runtime·sched);
n = runtime·gomaxprocs; n = runtime·gomaxprocs;
if(n > runtime·ncpu) if(n > runtime·ncpu)
n = runtime·ncpu; n = runtime·ncpu;
...@@ -679,9 +682,26 @@ runtime·gcprocs(void) ...@@ -679,9 +682,26 @@ runtime·gcprocs(void)
n = MaxGcproc; n = MaxGcproc;
if(n > runtime·sched.mwait+1) // one M is currently running if(n > runtime·sched.mwait+1) // one M is currently running
n = runtime·sched.mwait+1; n = runtime·sched.mwait+1;
runtime·unlock(&runtime·sched);
return n; return n;
} }
static bool
needaddgcproc(void)
{
int32 n;
runtime·lock(&runtime·sched);
n = runtime·gomaxprocs;
if(n > runtime·ncpu)
n = runtime·ncpu;
if(n > MaxGcproc)
n = MaxGcproc;
n -= runtime·sched.mwait+1; // one M is currently running
runtime·unlock(&runtime·sched);
return n > 0;
}
void void
runtime·helpgc(int32 nproc) runtime·helpgc(int32 nproc)
{ {
...@@ -740,20 +760,14 @@ void ...@@ -740,20 +760,14 @@ void
runtime·starttheworld(void) runtime·starttheworld(void)
{ {
M *mp; M *mp;
int32 max; bool add;
// Figure out how many CPUs GC could possibly use.
max = runtime·gomaxprocs;
if(max > runtime·ncpu)
max = runtime·ncpu;
if(max > MaxGcproc)
max = MaxGcproc;
add = needaddgcproc();
schedlock(); schedlock();
runtime·gcwaiting = 0; runtime·gcwaiting = 0;
setmcpumax(runtime·gomaxprocs); setmcpumax(runtime·gomaxprocs);
matchmg(); matchmg();
if(runtime·gcprocs() < max && canaddmcpu()) { if(add && canaddmcpu()) {
// If GC could have used another helper proc, start one now, // If GC could have used another helper proc, start one now,
// in the hope that it will be available next time. // in the hope that it will be available next time.
// It would have been even better to start it before the collection, // It would have been even better to start it before the collection,
...@@ -1171,9 +1185,8 @@ schedule(G *gp) ...@@ -1171,9 +1185,8 @@ schedule(G *gp)
if(m->profilehz != hz) if(m->profilehz != hz)
runtime·resetcpuprofiler(hz); runtime·resetcpuprofiler(hz);
if(gp->sched.pc == (byte*)runtime·goexit) { // kickoff if(gp->sched.pc == (byte*)runtime·goexit) // kickoff
runtime·gogocallfn(&gp->sched, gp->fnstart); runtime·gogocallfn(&gp->sched, gp->fnstart);
}
runtime·gogo(&gp->sched, 0); runtime·gogo(&gp->sched, 0);
} }
...@@ -1646,14 +1659,25 @@ runtime·mid(uint32 ret) ...@@ -1646,14 +1659,25 @@ runtime·mid(uint32 ret)
void void
runtime·NumGoroutine(intgo ret) runtime·NumGoroutine(intgo ret)
{ {
ret = runtime·sched.gcount; ret = runtime·gcount();
FLUSH(&ret); FLUSH(&ret);
} }
int32 int32
runtime·gcount(void) runtime·gcount(void)
{ {
return runtime·sched.gcount; G *gp;
int32 n, s;
n = 0;
runtime·lock(&runtime·sched);
for(gp = runtime·allg; gp; gp = gp->alllink) {
s = gp->status;
if(s == Grunnable || s == Grunning || s == Gsyscall || s == Gwaiting)
n++;
}
runtime·unlock(&runtime·sched);
return n;
} }
int32 int32
...@@ -1687,6 +1711,8 @@ runtime·sigprof(uint8 *pc, uint8 *sp, uint8 *lr, G *gp) ...@@ -1687,6 +1711,8 @@ runtime·sigprof(uint8 *pc, uint8 *sp, uint8 *lr, G *gp)
{ {
int32 n; int32 n;
if(m == nil || m->mcache == nil)
return;
if(prof.fn == nil || prof.hz == 0) if(prof.fn == nil || prof.hz == 0)
return; return;
......
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