Commit c19b373c authored by Russ Cox's avatar Russ Cox

runtime: cpu profiling support

R=r
CC=golang-dev
https://golang.org/cl/4306043
parent f9fc1ddf
...@@ -53,6 +53,7 @@ OFILES=\ ...@@ -53,6 +53,7 @@ OFILES=\
cgocall.$O\ cgocall.$O\
chan.$O\ chan.$O\
closure.$O\ closure.$O\
cpuprof.$O\
float.$O\ float.$O\
complex.$O\ complex.$O\
hashmap.$O\ hashmap.$O\
......
...@@ -18,8 +18,8 @@ void runtime·morestack(void); ...@@ -18,8 +18,8 @@ void runtime·morestack(void);
// as well as the runtime.Callers function (pcbuf != nil). // as well as the runtime.Callers function (pcbuf != nil).
// A little clunky to merge the two but avoids duplicating // A little clunky to merge the two but avoids duplicating
// the code and all its subtlety. // the code and all its subtlety.
static int32 int32
gentraceback(byte *pc0, byte *sp, G *g, int32 skip, uintptr *pcbuf, int32 max) runtime·gentraceback(byte *pc0, byte *sp, byte *lr0, G *g, int32 skip, uintptr *pcbuf, int32 max)
{ {
byte *p; byte *p;
int32 i, n, iter, sawnewstack; int32 i, n, iter, sawnewstack;
...@@ -28,6 +28,7 @@ gentraceback(byte *pc0, byte *sp, G *g, int32 skip, uintptr *pcbuf, int32 max) ...@@ -28,6 +28,7 @@ gentraceback(byte *pc0, byte *sp, G *g, int32 skip, uintptr *pcbuf, int32 max)
Stktop *stk; Stktop *stk;
Func *f; Func *f;
USED(lr0);
pc = (uintptr)pc0; pc = (uintptr)pc0;
lr = 0; lr = 0;
fp = nil; fp = nil;
...@@ -199,7 +200,7 @@ gentraceback(byte *pc0, byte *sp, G *g, int32 skip, uintptr *pcbuf, int32 max) ...@@ -199,7 +200,7 @@ gentraceback(byte *pc0, byte *sp, G *g, int32 skip, uintptr *pcbuf, int32 max)
void void
runtime·traceback(byte *pc0, byte *sp, byte*, G *g) runtime·traceback(byte *pc0, byte *sp, byte*, G *g)
{ {
gentraceback(pc0, sp, g, 0, nil, 100); runtime·gentraceback(pc0, sp, nil, g, 0, nil, 100);
} }
int32 int32
...@@ -211,7 +212,7 @@ runtime·callers(int32 skip, uintptr *pcbuf, int32 m) ...@@ -211,7 +212,7 @@ runtime·callers(int32 skip, uintptr *pcbuf, int32 m)
sp = (byte*)&skip; sp = (byte*)&skip;
pc = runtime·getcallerpc(&skip); pc = runtime·getcallerpc(&skip);
return gentraceback(pc, sp, g, skip, pcbuf, m); return runtime·gentraceback(pc, sp, nil, g, skip, pcbuf, m);
} }
static uintptr static uintptr
......
...@@ -15,7 +15,7 @@ void _divu(void); ...@@ -15,7 +15,7 @@ void _divu(void);
void _modu(void); void _modu(void);
static int32 static int32
gentraceback(byte *pc0, byte *sp, byte *lr0, G *g, int32 skip, uintptr *pcbuf, int32 max) runtime·gentraceback(byte *pc0, byte *sp, byte *lr0, G *g, int32 skip, uintptr *pcbuf, int32 max)
{ {
int32 i, n, iter; int32 i, n, iter;
uintptr pc, lr, tracepc, x; uintptr pc, lr, tracepc, x;
...@@ -189,11 +189,10 @@ gentraceback(byte *pc0, byte *sp, byte *lr0, G *g, int32 skip, uintptr *pcbuf, i ...@@ -189,11 +189,10 @@ gentraceback(byte *pc0, byte *sp, byte *lr0, G *g, int32 skip, uintptr *pcbuf, i
return n; return n;
} }
void void
runtime·traceback(byte *pc0, byte *sp, byte *lr, G *g) runtime·traceback(byte *pc0, byte *sp, byte *lr, G *g)
{ {
gentraceback(pc0, sp, lr, g, 0, nil, 100); runtime·gentraceback(pc0, sp, lr, g, 0, nil, 100);
} }
// func caller(n int) (pc uintptr, file string, line int, ok bool) // func caller(n int) (pc uintptr, file string, line int, ok bool)
...@@ -205,5 +204,5 @@ runtime·callers(int32 skip, uintptr *pcbuf, int32 m) ...@@ -205,5 +204,5 @@ runtime·callers(int32 skip, uintptr *pcbuf, int32 m)
sp = runtime·getcallersp(&skip); sp = runtime·getcallersp(&skip);
pc = runtime·getcallerpc(&skip); pc = runtime·getcallerpc(&skip);
return gentraceback(pc, sp, 0, g, skip, pcbuf, m); return runtime·gentraceback(pc, sp, 0, g, skip, pcbuf, m);
} }
This diff is collapsed.
...@@ -46,6 +46,11 @@ runtime·sighandler(int32 sig, Siginfo *info, void *context, G *gp) ...@@ -46,6 +46,11 @@ runtime·sighandler(int32 sig, Siginfo *info, void *context, G *gp)
mc = uc->uc_mcontext; mc = uc->uc_mcontext;
r = &mc->ss; r = &mc->ss;
if(sig == SIGPROF) {
runtime·sigprof((uint8*)r->eip, (uint8*)r->esp, nil, gp);
return;
}
if(gp != nil && (runtime·sigtab[sig].flags & SigPanic)) { if(gp != nil && (runtime·sigtab[sig].flags & SigPanic)) {
// Work around Leopard bug that doesn't set FPE_INTDIV. // Work around Leopard bug that doesn't set FPE_INTDIV.
// Look at instruction to see if it is a divide. // Look at instruction to see if it is a divide.
...@@ -126,31 +131,58 @@ runtime·signalstack(byte *p, int32 n) ...@@ -126,31 +131,58 @@ runtime·signalstack(byte *p, int32 n)
runtime·sigaltstack(&st, nil); runtime·sigaltstack(&st, nil);
} }
static void
sigaction(int32 i, void (*fn)(int32, Siginfo*, void*, G*), bool restart)
{
Sigaction sa;
runtime·memclr((byte*)&sa, sizeof sa);
sa.sa_flags = SA_SIGINFO|SA_ONSTACK;
if(restart)
sa.sa_flags |= SA_RESTART;
sa.sa_mask = ~0U;
sa.sa_tramp = (uintptr)runtime·sigtramp; // runtime·sigtramp's job is to call into real handler
sa.__sigaction_u.__sa_sigaction = (uintptr)fn;
runtime·sigaction(i, &sa, nil);
}
void void
runtime·initsig(int32 queue) runtime·initsig(int32 queue)
{ {
int32 i; int32 i;
static Sigaction sa; void *fn;
runtime·siginit(); runtime·siginit();
sa.sa_flags |= SA_SIGINFO|SA_ONSTACK;
sa.sa_mask = 0xFFFFFFFFU;
sa.sa_tramp = runtime·sigtramp; // runtime·sigtramp's job is to call into real handler
for(i = 0; i<NSIG; i++) { for(i = 0; i<NSIG; i++) {
if(runtime·sigtab[i].flags) { if(runtime·sigtab[i].flags) {
if((runtime·sigtab[i].flags & SigQueue) != queue) if((runtime·sigtab[i].flags & SigQueue) != queue)
continue; continue;
if(runtime·sigtab[i].flags & (SigCatch | SigQueue)) { if(runtime·sigtab[i].flags & (SigCatch | SigQueue))
sa.__sigaction_u.__sa_sigaction = runtime·sighandler; fn = runtime·sighandler;
} else {
sa.__sigaction_u.__sa_sigaction = runtime·sigignore;
}
if(runtime·sigtab[i].flags & SigRestart)
sa.sa_flags |= SA_RESTART;
else else
sa.sa_flags &= ~SA_RESTART; fn = runtime·sigignore;
runtime·sigaction(i, &sa, nil); sigaction(i, fn, (runtime·sigtab[i].flags & SigRestart) != 0);
} }
} }
} }
void
runtime·resetcpuprofiler(int32 hz)
{
Sigaction sa;
Itimerval it;
runtime·memclr((byte*)&it, sizeof it);
if(hz == 0) {
runtime·setitimer(ITIMER_PROF, &it, nil);
sigaction(SIGPROF, SIG_IGN, true);
} else {
sigaction(SIGPROF, runtime·sighandler, true);
it.it_interval.tv_sec = 0;
it.it_interval.tv_usec = 1000000 / hz;
it.it_value = it.it_interval;
runtime·setitimer(ITIMER_PROF, &it, nil);
}
m->profilehz = hz;
}
...@@ -54,6 +54,11 @@ runtime·sighandler(int32 sig, Siginfo *info, void *context, G *gp) ...@@ -54,6 +54,11 @@ runtime·sighandler(int32 sig, Siginfo *info, void *context, G *gp)
mc = uc->uc_mcontext; mc = uc->uc_mcontext;
r = &mc->ss; r = &mc->ss;
if(sig == SIGPROF) {
runtime·sigprof((uint8*)r->rip, (uint8*)r->rsp, nil, gp);
return;
}
if(gp != nil && (runtime·sigtab[sig].flags & SigPanic)) { if(gp != nil && (runtime·sigtab[sig].flags & SigPanic)) {
// Work around Leopard bug that doesn't set FPE_INTDIV. // Work around Leopard bug that doesn't set FPE_INTDIV.
// Look at instruction to see if it is a divide. // Look at instruction to see if it is a divide.
...@@ -136,31 +141,58 @@ runtime·signalstack(byte *p, int32 n) ...@@ -136,31 +141,58 @@ runtime·signalstack(byte *p, int32 n)
runtime·sigaltstack(&st, nil); runtime·sigaltstack(&st, nil);
} }
static void
sigaction(int32 i, void (*fn)(int32, Siginfo*, void*, G*), bool restart)
{
Sigaction sa;
runtime·memclr((byte*)&sa, sizeof sa);
sa.sa_flags = SA_SIGINFO|SA_ONSTACK;
if(restart)
sa.sa_flags |= SA_RESTART;
sa.sa_mask = ~0ULL;
sa.sa_tramp = (uintptr)runtime·sigtramp; // runtime·sigtramp's job is to call into real handler
sa.__sigaction_u.__sa_sigaction = (uintptr)fn;
runtime·sigaction(i, &sa, nil);
}
void void
runtime·initsig(int32 queue) runtime·initsig(int32 queue)
{ {
int32 i; int32 i;
static Sigaction sa; void *fn;
runtime·siginit(); runtime·siginit();
sa.sa_flags |= SA_SIGINFO|SA_ONSTACK;
sa.sa_mask = 0xFFFFFFFFU;
sa.sa_tramp = runtime·sigtramp; // runtime·sigtramp's job is to call into real handler
for(i = 0; i<NSIG; i++) { for(i = 0; i<NSIG; i++) {
if(runtime·sigtab[i].flags) { if(runtime·sigtab[i].flags) {
if((runtime·sigtab[i].flags & SigQueue) != queue) if((runtime·sigtab[i].flags & SigQueue) != queue)
continue; continue;
if(runtime·sigtab[i].flags & (SigCatch | SigQueue)) { if(runtime·sigtab[i].flags & (SigCatch | SigQueue))
sa.__sigaction_u.__sa_sigaction = runtime·sighandler; fn = runtime·sighandler;
} else {
sa.__sigaction_u.__sa_sigaction = runtime·sigignore;
}
if(runtime·sigtab[i].flags & SigRestart)
sa.sa_flags |= SA_RESTART;
else else
sa.sa_flags &= ~SA_RESTART; fn = runtime·sigignore;
runtime·sigaction(i, &sa, nil); sigaction(i, fn, (runtime·sigtab[i].flags & SigRestart) != 0);
} }
} }
} }
void
runtime·resetcpuprofiler(int32 hz)
{
Sigaction sa;
Itimerval it;
runtime·memclr((byte*)&it, sizeof it);
if(hz == 0) {
runtime·setitimer(ITIMER_PROF, &it, nil);
sigaction(SIGPROF, SIG_IGN, true);
} else {
sigaction(SIGPROF, runtime·sighandler, true);
it.it_interval.tv_sec = 0;
it.it_interval.tv_usec = 1000000 / hz;
it.it_value = it.it_interval;
runtime·setitimer(ITIMER_PROF, &it, nil);
}
m->profilehz = hz;
}
...@@ -92,4 +92,24 @@ func (r *MemProfileRecord) Stack() []uintptr { ...@@ -92,4 +92,24 @@ func (r *MemProfileRecord) Stack() []uintptr {
// where r.AllocBytes > 0 but r.AllocBytes == r.FreeBytes. // where r.AllocBytes > 0 but r.AllocBytes == r.FreeBytes.
// These are sites where memory was allocated, but it has all // These are sites where memory was allocated, but it has all
// been released back to the runtime. // been released back to the runtime.
// Most clients should use the runtime/pprof package or
// the testing package's -test.memprofile flag instead
// of calling MemProfile directly.
func MemProfile(p []MemProfileRecord, inuseZero bool) (n int, ok bool) func MemProfile(p []MemProfileRecord, inuseZero bool) (n int, ok bool)
// CPUProfile returns the next chunk of binary CPU profiling stack trace data,
// blocking until data is available. If profiling is turned off and all the profile
// data accumulated while it was on has been returned, CPUProfile returns nil.
// The caller must save the returned data before calling CPUProfile again.
// Most clients should use the runtime/pprof package or
// the testing package's -test.cpuprofile flag instead of calling
// CPUProfile directly.
func CPUProfile() []byte
// SetCPUProfileRate sets the CPU profiling rate to hz samples per second.
// If hz <= 0, SetCPUProfileRate turns off profiling.
// If the profiler is on, the rate cannot be changed without first turning it off.
// Most clients should use the runtime/pprof package or
// the testing package's -test.cpuprofile flag instead of calling
// SetCPUProfileRate directly.
func SetCPUProfileRate(hz int)
...@@ -54,6 +54,11 @@ runtime·sighandler(int32 sig, Siginfo *info, void *context, G *gp) ...@@ -54,6 +54,11 @@ runtime·sighandler(int32 sig, Siginfo *info, void *context, G *gp)
uc = context; uc = context;
r = &uc->uc_mcontext; r = &uc->uc_mcontext;
if(sig == SIGPROF) {
runtime·sigprof((uint8*)r->mc_eip, (uint8*)r->mc_esp, nil, gp);
return;
}
if(gp != nil && (runtime·sigtab[sig].flags & SigPanic)) { if(gp != nil && (runtime·sigtab[sig].flags & SigPanic)) {
// Make it look like a call to the signal func. // Make it look like a call to the signal func.
// Have to pass arguments out of band since // Have to pass arguments out of band since
...@@ -122,32 +127,57 @@ runtime·signalstack(byte *p, int32 n) ...@@ -122,32 +127,57 @@ runtime·signalstack(byte *p, int32 n)
runtime·sigaltstack(&st, nil); runtime·sigaltstack(&st, nil);
} }
static void
sigaction(int32 i, void (*fn)(int32, Siginfo*, void*, G*), bool restart)
{
Sigaction sa;
runtime·memclr((byte*)&sa, sizeof sa);
sa.sa_flags = SA_SIGINFO|SA_ONSTACK;
if(restart)
sa.sa_flags |= SA_RESTART;
sa.sa_mask = ~0ULL;
sa.__sigaction_u.__sa_sigaction = (uintptr)fn;
runtime·sigaction(i, &sa, nil);
}
void void
runtime·initsig(int32 queue) runtime·initsig(int32 queue)
{ {
static Sigaction sa; int32 i;
void *fn;
runtime·siginit(); runtime·siginit();
int32 i; for(i = 0; i<NSIG; i++) {
sa.sa_flags |= SA_ONSTACK | SA_SIGINFO;
sa.sa_mask = ~0x0ull;
for(i = 0; i < NSIG; i++) {
if(runtime·sigtab[i].flags) { if(runtime·sigtab[i].flags) {
if((runtime·sigtab[i].flags & SigQueue) != queue) if((runtime·sigtab[i].flags & SigQueue) != queue)
continue; continue;
if(runtime·sigtab[i].flags & (SigCatch | SigQueue)) if(runtime·sigtab[i].flags & (SigCatch | SigQueue))
sa.__sigaction_u.__sa_sigaction = (void*) runtime·sigtramp; fn = runtime·sighandler;
else else
sa.__sigaction_u.__sa_sigaction = (void*) runtime·sigignore; fn = runtime·sigignore;
sigaction(i, fn, (runtime·sigtab[i].flags & SigRestart) != 0);
if(runtime·sigtab[i].flags & SigRestart)
sa.sa_flags |= SA_RESTART;
else
sa.sa_flags &= ~SA_RESTART;
runtime·sigaction(i, &sa, nil);
} }
} }
} }
void
runtime·resetcpuprofiler(int32 hz)
{
Sigaction sa;
Itimerval it;
runtime·memclr((byte*)&it, sizeof it);
if(hz == 0) {
runtime·setitimer(ITIMER_PROF, &it, nil);
sigaction(SIGPROF, SIG_IGN, true);
} else {
sigaction(SIGPROF, runtime·sighandler, true);
it.it_interval.tv_sec = 0;
it.it_interval.tv_usec = 1000000 / hz;
it.it_value = it.it_interval;
runtime·setitimer(ITIMER_PROF, &it, nil);
}
m->profilehz = hz;
}
...@@ -62,6 +62,11 @@ runtime·sighandler(int32 sig, Siginfo *info, void *context, G *gp) ...@@ -62,6 +62,11 @@ runtime·sighandler(int32 sig, Siginfo *info, void *context, G *gp)
uc = context; uc = context;
r = &uc->uc_mcontext; r = &uc->uc_mcontext;
if(sig == SIGPROF) {
runtime·sigprof((uint8*)r->mc_rip, (uint8*)r->mc_rsp, nil, gp);
return;
}
if(gp != nil && (runtime·sigtab[sig].flags & SigPanic)) { if(gp != nil && (runtime·sigtab[sig].flags & SigPanic)) {
// Make it look like a call to the signal func. // Make it look like a call to the signal func.
// Have to pass arguments out of band since // Have to pass arguments out of band since
...@@ -130,32 +135,57 @@ runtime·signalstack(byte *p, int32 n) ...@@ -130,32 +135,57 @@ runtime·signalstack(byte *p, int32 n)
runtime·sigaltstack(&st, nil); runtime·sigaltstack(&st, nil);
} }
static void
sigaction(int32 i, void (*fn)(int32, Siginfo*, void*, G*), bool restart)
{
Sigaction sa;
runtime·memclr((byte*)&sa, sizeof sa);
sa.sa_flags = SA_SIGINFO|SA_ONSTACK;
if(restart)
sa.sa_flags |= SA_RESTART;
sa.sa_mask = ~0ULL;
sa.__sigaction_u.__sa_sigaction = (uintptr)fn;
runtime·sigaction(i, &sa, nil);
}
void void
runtime·initsig(int32 queue) runtime·initsig(int32 queue)
{ {
static Sigaction sa; int32 i;
void *fn;
runtime·siginit(); runtime·siginit();
int32 i; for(i = 0; i<NSIG; i++) {
sa.sa_flags |= SA_ONSTACK | SA_SIGINFO;
sa.sa_mask = ~0x0ull;
for(i = 0; i < NSIG; i++) {
if(runtime·sigtab[i].flags) { if(runtime·sigtab[i].flags) {
if((runtime·sigtab[i].flags & SigQueue) != queue) if((runtime·sigtab[i].flags & SigQueue) != queue)
continue; continue;
if(runtime·sigtab[i].flags & (SigCatch | SigQueue)) if(runtime·sigtab[i].flags & (SigCatch | SigQueue))
sa.__sigaction_u.__sa_sigaction = (void*) runtime·sigtramp; fn = runtime·sighandler;
else else
sa.__sigaction_u.__sa_sigaction = (void*) runtime·sigignore; fn = runtime·sigignore;
sigaction(i, fn, (runtime·sigtab[i].flags & SigRestart) != 0);
if(runtime·sigtab[i].flags & SigRestart)
sa.sa_flags |= SA_RESTART;
else
sa.sa_flags &= ~SA_RESTART;
runtime·sigaction(i, &sa, nil);
} }
} }
} }
void
runtime·resetcpuprofiler(int32 hz)
{
Sigaction sa;
Itimerval it;
runtime·memclr((byte*)&it, sizeof it);
if(hz == 0) {
runtime·setitimer(ITIMER_PROF, &it, nil);
sigaction(SIGPROF, SIG_IGN, true);
} else {
sigaction(SIGPROF, runtime·sighandler, true);
it.it_interval.tv_sec = 0;
it.it_interval.tv_usec = 1000000 / hz;
it.it_value = it.it_interval;
runtime·setitimer(ITIMER_PROF, &it, nil);
}
m->profilehz = hz;
}
...@@ -51,6 +51,11 @@ runtime·sighandler(int32 sig, Siginfo *info, void *context, G *gp) ...@@ -51,6 +51,11 @@ runtime·sighandler(int32 sig, Siginfo *info, void *context, G *gp)
uc = context; uc = context;
r = &uc->uc_mcontext; r = &uc->uc_mcontext;
if(sig == SIGPROF) {
runtime·sigprof((uint8*)r->eip, (uint8*)r->esp, nil, gp);
return;
}
if(gp != nil && (runtime·sigtab[sig].flags & SigPanic)) { if(gp != nil && (runtime·sigtab[sig].flags & SigPanic)) {
// Make it look like a call to the signal func. // Make it look like a call to the signal func.
// Have to pass arguments out of band since // Have to pass arguments out of band since
...@@ -114,30 +119,59 @@ runtime·signalstack(byte *p, int32 n) ...@@ -114,30 +119,59 @@ runtime·signalstack(byte *p, int32 n)
runtime·sigaltstack(&st, nil); runtime·sigaltstack(&st, nil);
} }
static void
sigaction(int32 i, void (*fn)(int32, Siginfo*, void*, G*), bool restart)
{
Sigaction sa;
runtime·memclr((byte*)&sa, sizeof sa);
sa.sa_flags = SA_ONSTACK | SA_SIGINFO | SA_RESTORER;
if(restart)
sa.sa_flags |= SA_RESTART;
sa.sa_mask = ~0ULL;
sa.sa_restorer = (void*)runtime·sigreturn;
if(fn == runtime·sighandler)
fn = (void*)runtime·sigtramp;
sa.k_sa_handler = fn;
runtime·rt_sigaction(i, &sa, nil, 8);
}
void void
runtime·initsig(int32 queue) runtime·initsig(int32 queue)
{ {
static Sigaction sa; int32 i;
void *fn;
runtime·siginit(); runtime·siginit();
int32 i;
sa.sa_flags = SA_ONSTACK | SA_SIGINFO | SA_RESTORER;
sa.sa_mask = 0xFFFFFFFFFFFFFFFFULL;
sa.sa_restorer = (void*)runtime·sigreturn;
for(i = 0; i<NSIG; i++) { for(i = 0; i<NSIG; i++) {
if(runtime·sigtab[i].flags) { if(runtime·sigtab[i].flags) {
if((runtime·sigtab[i].flags & SigQueue) != queue) if((runtime·sigtab[i].flags & SigQueue) != queue)
continue; continue;
if(runtime·sigtab[i].flags & (SigCatch | SigQueue)) if(runtime·sigtab[i].flags & (SigCatch | SigQueue))
sa.k_sa_handler = (void*)runtime·sigtramp; fn = runtime·sighandler;
else else
sa.k_sa_handler = (void*)runtime·sigignore; fn = runtime·sigignore;
if(runtime·sigtab[i].flags & SigRestart) sigaction(i, fn, (runtime·sigtab[i].flags & SigRestart) != 0);
sa.sa_flags |= SA_RESTART;
else
sa.sa_flags &= ~SA_RESTART;
runtime·rt_sigaction(i, &sa, nil, 8);
} }
} }
} }
void
runtime·resetcpuprofiler(int32 hz)
{
Itimerval it;
runtime·memclr((byte*)&it, sizeof it);
if(hz == 0) {
runtime·setitimer(ITIMER_PROF, &it, nil);
sigaction(SIGPROF, SIG_IGN, true);
} else {
sigaction(SIGPROF, runtime·sighandler, true);
it.it_interval.tv_sec = 0;
it.it_interval.tv_usec = 1000000 / hz;
it.it_value = it.it_interval;
runtime·setitimer(ITIMER_PROF, &it, nil);
}
m->profilehz = hz;
}
...@@ -61,6 +61,11 @@ runtime·sighandler(int32 sig, Siginfo *info, void *context, G *gp) ...@@ -61,6 +61,11 @@ runtime·sighandler(int32 sig, Siginfo *info, void *context, G *gp)
mc = &uc->uc_mcontext; mc = &uc->uc_mcontext;
r = (Sigcontext*)mc; // same layout, more conveient names r = (Sigcontext*)mc; // same layout, more conveient names
if(sig == SIGPROF) {
runtime·sigprof((uint8*)r->rip, (uint8*)r->rsp, nil, gp);
return;
}
if(gp != nil && (runtime·sigtab[sig].flags & SigPanic)) { if(gp != nil && (runtime·sigtab[sig].flags & SigPanic)) {
// Make it look like a call to the signal func. // Make it look like a call to the signal func.
// Have to pass arguments out of band since // Have to pass arguments out of band since
...@@ -124,30 +129,59 @@ runtime·signalstack(byte *p, int32 n) ...@@ -124,30 +129,59 @@ runtime·signalstack(byte *p, int32 n)
runtime·sigaltstack(&st, nil); runtime·sigaltstack(&st, nil);
} }
static void
sigaction(int32 i, void (*fn)(int32, Siginfo*, void*, G*), bool restart)
{
Sigaction sa;
runtime·memclr((byte*)&sa, sizeof sa);
sa.sa_flags = SA_ONSTACK | SA_SIGINFO | SA_RESTORER;
if(restart)
sa.sa_flags |= SA_RESTART;
sa.sa_mask = ~0ULL;
sa.sa_restorer = (void*)runtime·sigreturn;
if(fn == runtime·sighandler)
fn = (void*)runtime·sigtramp;
sa.sa_handler = fn;
runtime·rt_sigaction(i, &sa, nil, 8);
}
void void
runtime·initsig(int32 queue) runtime·initsig(int32 queue)
{ {
static Sigaction sa; int32 i;
void *fn;
runtime·siginit(); runtime·siginit();
int32 i;
sa.sa_flags = SA_ONSTACK | SA_SIGINFO | SA_RESTORER;
sa.sa_mask = 0xFFFFFFFFFFFFFFFFULL;
sa.sa_restorer = (void*)runtime·sigreturn;
for(i = 0; i<NSIG; i++) { for(i = 0; i<NSIG; i++) {
if(runtime·sigtab[i].flags) { if(runtime·sigtab[i].flags) {
if((runtime·sigtab[i].flags & SigQueue) != queue) if((runtime·sigtab[i].flags & SigQueue) != queue)
continue; continue;
if(runtime·sigtab[i].flags & (SigCatch | SigQueue)) if(runtime·sigtab[i].flags & (SigCatch | SigQueue))
sa.sa_handler = (void*)runtime·sigtramp; fn = runtime·sighandler;
else else
sa.sa_handler = (void*)runtime·sigignore; fn = runtime·sigignore;
if(runtime·sigtab[i].flags & SigRestart) sigaction(i, fn, (runtime·sigtab[i].flags & SigRestart) != 0);
sa.sa_flags |= SA_RESTART;
else
sa.sa_flags &= ~SA_RESTART;
runtime·rt_sigaction(i, &sa, nil, 8);
} }
} }
} }
void
runtime·resetcpuprofiler(int32 hz)
{
Itimerval it;
runtime·memclr((byte*)&it, sizeof it);
if(hz == 0) {
runtime·setitimer(ITIMER_PROF, &it, nil);
sigaction(SIGPROF, SIG_IGN, true);
} else {
sigaction(SIGPROF, runtime·sighandler, true);
it.it_interval.tv_sec = 0;
it.it_interval.tv_usec = 1000000 / hz;
it.it_value = it.it_interval;
runtime·setitimer(ITIMER_PROF, &it, nil);
}
m->profilehz = hz;
}
...@@ -58,6 +58,11 @@ runtime·sighandler(int32 sig, Siginfo *info, void *context, G *gp) ...@@ -58,6 +58,11 @@ runtime·sighandler(int32 sig, Siginfo *info, void *context, G *gp)
uc = context; uc = context;
r = &uc->uc_mcontext; r = &uc->uc_mcontext;
if(sig == SIGPROF) {
runtime·sigprof((uint8*)r->arm_pc, (uint8*)r->arm_sp, (uint8*)r->arm_lr, gp);
return;
}
if(gp != nil && (runtime·sigtab[sig].flags & SigPanic)) { if(gp != nil && (runtime·sigtab[sig].flags & SigPanic)) {
// Make it look like a call to the signal func. // Make it look like a call to the signal func.
// Have to pass arguments out of band since // Have to pass arguments out of band since
...@@ -119,31 +124,58 @@ runtime·signalstack(byte *p, int32 n) ...@@ -119,31 +124,58 @@ runtime·signalstack(byte *p, int32 n)
runtime·sigaltstack(&st, nil); runtime·sigaltstack(&st, nil);
} }
static void
sigaction(int32 i, void (*fn)(int32, Siginfo*, void*, G*), bool restart)
{
Sigaction sa;
runtime·memclr((byte*)&sa, sizeof sa);
sa.sa_flags = SA_ONSTACK | SA_SIGINFO | SA_RESTORER;
if(restart)
sa.sa_flags |= SA_RESTART;
sa.sa_mask = ~0ULL;
sa.sa_restorer = (void*)runtime·sigreturn;
sa.k_sa_handler = fn;
runtime·rt_sigaction(i, &sa, nil, 8);
}
void void
runtime·initsig(int32 queue) runtime·initsig(int32 queue)
{ {
static Sigaction sa; int32 i;
void *fn;
runtime·siginit(); runtime·siginit();
int32 i;
sa.sa_flags = SA_ONSTACK | SA_SIGINFO | SA_RESTORER;
sa.sa_mask.sig[0] = 0xFFFFFFFF;
sa.sa_mask.sig[1] = 0xFFFFFFFF;
sa.sa_restorer = (void*)runtime·sigreturn;
for(i = 0; i<NSIG; i++) { for(i = 0; i<NSIG; i++) {
if(runtime·sigtab[i].flags) { if(runtime·sigtab[i].flags) {
if((runtime·sigtab[i].flags & SigQueue) != queue) if((runtime·sigtab[i].flags & SigQueue) != queue)
continue; continue;
if(runtime·sigtab[i].flags & (SigCatch | SigQueue)) if(runtime·sigtab[i].flags & (SigCatch | SigQueue))
sa.sa_handler = (void*)runtime·sigtramp; fn = runtime·sighandler;
else else
sa.sa_handler = (void*)runtime·sigignore; fn = runtime·sigignore;
if(runtime·sigtab[i].flags & SigRestart) sigaction(i, fn, (runtime·sigtab[i].flags & SigRestart) != 0);
sa.sa_flags |= SA_RESTART;
else
sa.sa_flags &= ~SA_RESTART;
runtime·rt_sigaction(i, &sa, nil, 8);
} }
} }
} }
void
runtime·resetcpuprofiler(int32 hz)
{
Sigaction sa;
Itimerval it;
runtime·memclr((byte*)&it, sizeof it);
if(hz == 0) {
runtime·setitimer(ITIMER_PROF, &it, nil);
sigaction(SIGPROF, SIG_IGN, true);
} else {
sigaction(SIGPROF, runtime·sighandler, true);
it.it_interval.tv_sec = 0;
it.it_interval.tv_usec = 1000000 / hz;
it.it_value = it.it_interval;
runtime·setitimer(ITIMER_PROF, &it, nil);
}
m->profilehz = hz;
}
...@@ -14,3 +14,11 @@ runtime·signame(int32) ...@@ -14,3 +14,11 @@ runtime·signame(int32)
{ {
return runtime·emptystring; return runtime·emptystring;
} }
void
runtime·resetcpuprofiler(int32 hz)
{
// TODO: Enable profiling interrupts.
m->profilehz = hz;
}
...@@ -70,6 +70,7 @@ struct Sched { ...@@ -70,6 +70,7 @@ struct Sched {
int32 msyscall; // number of ms in system calls int32 msyscall; // number of ms in system calls
int32 predawn; // running initialization, don't run new gs. int32 predawn; // running initialization, don't run new gs.
int32 profilehz; // cpu profiling rate
Note stopped; // one g can wait here for ms to stop Note stopped; // one g can wait here for ms to stop
int32 waitstop; // after setting this flag int32 waitstop; // after setting this flag
...@@ -96,9 +97,6 @@ static void matchmg(void); // match ms to gs ...@@ -96,9 +97,6 @@ static void matchmg(void); // match ms to gs
static void readylocked(G*); // ready, but sched is locked static void readylocked(G*); // ready, but sched is locked
static void mnextg(M*, G*); static void mnextg(M*, G*);
// Scheduler loop.
static void scheduler(void);
// The bootstrap sequence is: // The bootstrap sequence is:
// //
// call osinit // call osinit
...@@ -529,6 +527,8 @@ matchmg(void) ...@@ -529,6 +527,8 @@ matchmg(void)
static void static void
schedule(G *gp) schedule(G *gp)
{ {
int32 hz;
schedlock(); schedlock();
if(gp != nil) { if(gp != nil) {
if(runtime·sched.predawn) if(runtime·sched.predawn)
...@@ -574,6 +574,12 @@ schedule(G *gp) ...@@ -574,6 +574,12 @@ schedule(G *gp)
gp->status = Grunning; gp->status = Grunning;
m->curg = gp; m->curg = gp;
gp->m = m; gp->m = m;
// Check whether the profiler needs to be turned on or off.
hz = runtime·sched.profilehz;
if(m->profilehz != hz)
runtime·resetcpuprofiler(hz);
if(gp->sched.pc == (byte*)runtime·goexit) { // kickoff if(gp->sched.pc == (byte*)runtime·goexit) { // kickoff
runtime·gogocall(&gp->sched, (void(*)(void))gp->entry); runtime·gogocall(&gp->sched, (void(*)(void))gp->entry);
} }
...@@ -640,7 +646,7 @@ runtime·exitsyscall(void) ...@@ -640,7 +646,7 @@ runtime·exitsyscall(void)
runtime·sched.msyscall--; runtime·sched.msyscall--;
runtime·sched.mcpu++; runtime·sched.mcpu++;
// Fast path - if there's room for this m, we're done. // Fast path - if there's room for this m, we're done.
if(runtime·sched.mcpu <= runtime·sched.mcpumax) { if(m->profilehz == runtime·sched.profilehz && runtime·sched.mcpu <= runtime·sched.mcpumax) {
g->status = Grunning; g->status = Grunning;
schedunlock(); schedunlock();
return; return;
...@@ -1251,3 +1257,57 @@ runtime·badmcall2(void) // called from assembly ...@@ -1251,3 +1257,57 @@ runtime·badmcall2(void) // called from assembly
{ {
runtime·throw("runtime: mcall function returned"); runtime·throw("runtime: mcall function returned");
} }
static struct {
Lock;
void (*fn)(uintptr*, int32);
int32 hz;
uintptr pcbuf[100];
} prof;
void
runtime·sigprof(uint8 *pc, uint8 *sp, uint8 *lr, G *gp)
{
int32 n;
if(prof.fn == nil || prof.hz == 0)
return;
runtime·lock(&prof);
if(prof.fn == nil) {
runtime·unlock(&prof);
return;
}
n = runtime·gentraceback(pc, sp, lr, gp, 0, prof.pcbuf, nelem(prof.pcbuf));
if(n > 0)
prof.fn(prof.pcbuf, n);
runtime·unlock(&prof);
}
void
runtime·setcpuprofilerate(void (*fn)(uintptr*, int32), int32 hz)
{
// Force sane arguments.
if(hz < 0)
hz = 0;
if(hz == 0)
fn = nil;
if(fn == nil)
hz = 0;
// Stop profiler on this cpu so that it is safe to lock prof.
// if a profiling signal came in while we had prof locked,
// it would deadlock.
runtime·resetcpuprofiler(0);
runtime·lock(&prof);
prof.fn = fn;
prof.hz = hz;
runtime·unlock(&prof);
runtime·lock(&runtime·sched);
runtime·sched.profilehz = hz;
runtime·unlock(&runtime·sched);
if(hz != 0)
runtime·resetcpuprofiler(hz);
}
...@@ -225,6 +225,7 @@ struct M ...@@ -225,6 +225,7 @@ struct M
int32 nomemprof; int32 nomemprof;
int32 waitnextg; int32 waitnextg;
int32 dying; int32 dying;
int32 profilehz;
Note havenextg; Note havenextg;
G* nextg; G* nextg;
M* alllink; // on allm M* alllink; // on allm
...@@ -453,9 +454,13 @@ void runtime·siginit(void); ...@@ -453,9 +454,13 @@ void runtime·siginit(void);
bool runtime·sigsend(int32 sig); bool runtime·sigsend(int32 sig);
void runtime·gettime(int64*, int32*); void runtime·gettime(int64*, int32*);
int32 runtime·callers(int32, uintptr*, int32); int32 runtime·callers(int32, uintptr*, int32);
int32 runtime·gentraceback(byte*, byte*, byte*, G*, int32, uintptr*, int32);
int64 runtime·nanotime(void); int64 runtime·nanotime(void);
void runtime·dopanic(int32); void runtime·dopanic(int32);
void runtime·startpanic(void); void runtime·startpanic(void);
void runtime·sigprof(uint8 *pc, uint8 *sp, uint8 *lr, G *gp);
void runtime·resetcpuprofiler(int32);
void runtime·setcpuprofilerate(void(*)(uintptr*, int32), int32);
#pragma varargck argpos runtime·printf 1 #pragma varargck argpos runtime·printf 1
#pragma varargck type "d" int32 #pragma varargck type "d" int32
......
...@@ -88,3 +88,11 @@ runtime·sighandler(ExceptionRecord *info, void *frame, Context *r) ...@@ -88,3 +88,11 @@ runtime·sighandler(ExceptionRecord *info, void *frame, Context *r)
runtime·exit(2); runtime·exit(2);
return 0; return 0;
} }
void
runtime·resetcpuprofiler(int32 hz)
{
// TODO: Enable profiling interrupts.
m->profilehz = hz;
}
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